一、概述
1. 数据库主从概念、优点、用途
主从数据库是什么意思呢,主是主库的意思,从是从库的意思。数据库主库对外提供读写的操作,从库对外提供读的操作。
数据库为什么需要主从架构呢?
高可用,实时灾备,用于故障切换。比如主库挂了,可以切从库。读写分离,提供查询服务,减少主库压力,提升性能备份数据,避免影响业务。
2. 数据库主从复制原理
主从复制原理,简言之,分三步曲进行:
①主数据库有个 binlog 二进制文件,记录了所有增删改 SQL 语句;
②(binlog线程)从数据库把主数据库的binlog文件的 SQL 语句复制到自己的中继日志relaylog;
③(io线程)从数据库的relaylog重做日志文件,再执行一次这些sql语句。
(sql执行线程)详细的主从复制过程如图:
二、安装mysql数据库
在主从服务器上均需要完成以下工作:
2.1 安装mysql数据库(基于docker)
docker search mysql
docker pull mysql:8.0.23
# 1.启一个该版本mysql的容器
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.23mkdir -p /usr/rdc/mysql-8.0.23/conf
docker cp mysql:/etc/mysql/my.cnf /usr/rdc/mysql-8.0.23/conf
chmod -R 777 /usr/rdc/mysql-8.0.23/conf
chmod -R 644 /usr/rdc/mysql-8.0.23/conf/my.cnf
4.打开配置文件查看
cat /usr/rdc/mysql-8.0.23/conf/my.cnf #配置文件为默认的
docker stop mysql
docker rm mysql
由配置文件可知:数据文件位置为/var/lib/mysql, 自定义配置文件可以放到容器的/etc/mysql/conf.d目录下,日志文件目录为 /var/log
记住以上三个文件所在位置,下面创建容器做数据卷映射时需要一一对应。
二、配置master库
1.在主服务器中编辑my.cnf文件
cat > /usr/rdc/mysql-8.0.23/conf/my.cnf << EOF
[mysqld]
# 设置数据库引擎为INNODB
default-storage-engine=INNODB
# 设置授权访问的加密策略
default_authentication_plugin=mysql_native_password# 主从复制配置.start
# 服务器ID
server-id=2013306
# 启用二进制日志
log-bin=master-bin
# 设置logbin格式:STATEMENT(同步SQL脚本) / ROW(同步数据行) / MIXED(混合同步)
binlog_format=MIXED
# 设置日志最长保存时间
expire_logs_days=30
# 0-读写,1-只读
read-only=0# 设置忽略同步的数据库
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys# 设置需要同步的数据库
#binlog-do-db=pmonitor
#binlog-do-db=ucoal# 主从复制配置.end
EOF
2.创建容器(映射数据卷)
docker run -p 3306:3306 --privileged=true --restart=always --name mysql -v /usr/rdc/mysql-8.0.23/conf:/etc/mysql/conf.d -v /usr/rdc/mysql-8.0.23/logs:/var/log/mysql -v /usr/rdc/mysql-8.0.23/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.23
3.进入容器访问mysql
#查看容器id
docker ps#进入容器
docker exec -it mysql /bin/bash
root@35901b71cff7:/# mysql -uroot -p
mysql: [Warning] Skipping '!includedir /etc/mysql/conf.d/' directive as maximum include recursion level was reached in file /etc/mysql/conf.d/my.cnf at line 29.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)mysql>
4.创建从机访问主库使用的账号
#访问mysql数据库
mysql -uroot -proot#创建账号
create user 'slave1'@'%' identified by 'slave1';
#授权
grant replication slave on *.* to 'slave1'@'%';
#更新用户密码方案(一定要执行否则无法远程访问)
alter user 'slave1'@'%' identified with mysql_native_password by 'slave1';#刷新
flush privileges;
5.重启容器
docker restart mysql
6.获取日志文件名和偏移量
执行一下命令:
#访问mysql数据库
docker exec -it mysql /bin/bash #进入容器
mysql -uroot -proot #密码是root
show master status;
三、配置slave库
1.在从服务器中编辑my.cnf文件
mkdir -p /usr/rdc/mysql-8.0.23/conf/
touch /usr/rdc/mysql-8.0.23/conf/my.cnf
chmod -R 777 /usr/rdc/mysql-8.0.23/conf
chmod -R 644 /usr/rdc/mysql-8.0.23/conf/my.cnf
cat > /usr/rdc/mysql-8.0.23/conf/my.cnf << EOF
[mysqld]
# 设置数据库存储引擎为INNODB
default-storage-engine=INNODB
# 设置授权验证的加密策略
default_authentication_plugin=mysql_native_password# 主从复制配置.start
# 服务器ID
server-id=2023306# 启用中继日志
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
# 设置日志最长保存时间
expire_logs_days=30
# 0-读写,1-只读;slave设置为只读(具有super权限的用户除外)
read_only=1# 开启二进制日志功能,以便本机可以作为其它Slave的Master时使用
log-bin=slave-bin
# 设置logbin格式:STATEMENT(同步SQL脚本) / ROW(同步数据行) / MIXED(混合同步)
binlog_format=MIXED
# 1表示slave将复制事件写进自己的二进制日志
log_slave_updates=1# 设置允许复制的库
# replicate-do-db=pmonitor-cloud
# replicate-do-db=ucoal# 设置忽略复制的库
# replicate-ignore-db=mysql
# replicate-ignore-db=information_schema
# replicate-ignore-db=performance_schema#主从复制配置.end
EOF
docker run -p 3306:3306 --privileged=true --restart=always --name mysql -v /usr/rdc/mysql-8.0.23/conf:/etc/mysql/conf.d -v /usr/rdc/mysql-8.0.23/logs:/var/log/mysql -v /usr/rdc/mysql-8.0.23/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.23
#进入容器
docker exec -it mysql /bin/bash#访问mysql数据库
mysql -uroot -proot #密码是root
stop slave; #先停止从服务器线程
change master to master_host='192.168.186.129', master_port=3306, master_user='slave1', master_password='slave1', master_log_file='master-bin.000001', master_log_pos=156, get_master_public_key=1; #从服务器的IP
show master status;
start slave;
show master status;
docker restart mysql
docker exec -it mysql /bin/bash
mysql -uroot -proot #密码是root
show slave status \G;
mysql> show slave status \G;
*************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: 192.168.186.129Master_User: slave1Master_Port: 3306Connect_Retry: 60Master_Log_File: master-bin.000001Read_Master_Log_Pos: 156Relay_Log_File: 00d49616dbf7-relay-bin.000002Relay_Log_Pos: 4Relay_Master_Log_File: master-bin.000001Slave_IO_Running: ConnectingSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 156Relay_Log_Space: 156Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 1045Last_IO_Error: error connecting to master 'slave1@192.168.186.129:3306' - retry-time: 60 retries: 1 message: Access denied for user 'slave1'@'172.17.0.1' (using password: YES)Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: 230527 03:09:26Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 1Network_Namespace:
1 row in set, 1 warning (0.00 sec)ERROR:
No query specifiedmysql>
使用上述命令查看状态,Slave_IO_Running、Slave_SQL_Running两个参数均为YES时,表示集群状态正常。
否则,需要根据 Last_IO_Error 或 Last_SQL_Error的报错信息进行排查。
#重启主从服务器的mysql容器
docker restart mysql
#登录主服务器查看配置是否生效
docker exec -it mysql /bin/bash
mysql -uroot -proot
show variables like 'server_id'; ##查看server-id
show global variables like '%log_bin%'; ##查看binlog日志文件名称
发现server-id,与binlog日志文件均未按配置文件要求生成。
转载:https://blog.csdn.net/hualinger/article/details/131292136