MySQL8主从复制(一主一从)配置搭建详解
准备两台虚拟机服务器
操作系统 openEuler
一主一从
角色 | 主机名 | IP地址 |
---|---|---|
主库 | master | 192.168.1.201 |
从库 | slave | 192.168.1.202 |
分别安装MySQL
下载MySQL
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.31-1.el8.x86_64.rpm-bundle.tar
创建安装包目录
mkdir mysql
释放安装包到安装包目录
tar -xvf mysql-8.0.31-1.el8.x86_64.rpm-bundle.tar -C mysql
切换到MySQL安装包目录
cd mysql
安装MySQL
rpm -ivh *.rpm --force --nodeps
启动MySQL
systemctl start mysqld
设置MySQL开机自启动
systemctl enable mysqld
在服务器初始启动时,假设服务器的数据目录为空,会发生以下情况:
-
服务器已初始化。
-
SSL 证书和密钥文件在数据目录中生成。
-
validate_password
已安装并启用。 -
创建了一个超级用户帐户
'root'@'localhost
。超级用户的密码已设置并存储在错误日志文件中。要显示它,请使用以下命令:sudo grep 'temporary password' /var/log/mysqld.log
通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改 root 密码:
mysql -uroot -p
修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Lihaozhe!!@@1122';
FLUSH PRIVILEGES;
设置远程访问地址
update mysql.user set host = '%',plugin='mysql_native_password' where user='root';
FLUSH PRIVILEGES;
退出MySQL
exit;
重启MySQL
systemctl restart mysqld
防火墙开放端口
firewall-cmd --zone=public --add-port=3306/tcp --add-port=33060/tcp --permanent
重启防火墙
firewall-cmd --reload
主库配置
在主从库中分别建一个你需要同步的测试库,比如我这里创建的库叫lihaozhe,然后在这两个库中分别建一个测试表,都是空表即可。
修改配置文件
vim /etc/my.cnf
追加以下内容:
实验配置
server-id=201
log-bin=mysql-bin
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
生产环境配置
还可以配置一些其他参数:
log_bin_index(bin日志文件索引位置),
expire_logs_days 日志多少天过期,binlog_format 等。
server-id=201 #服务器 id,随意,但要唯一
log-bin=mysql-bin #二进制文件存放路径
binlog-do-db=lihaozhe #待同步的数据库日志
binlog-ignore-db=mysql #不同步的数据库日志
创建账号用于主从复制
#创建用户 我这里用户名为copyuser,注意这里的ip是从库服务器的ip
CREATE USER 'lihaozhe'@'%' IDENTIFIED WITH mysql_native_password BY 'Lihaozhe!!@@6633';
#给主从复制账号授权
grant replication slave on *.* to 'lihaozhe'@'%';
重启MySQL
systemctl restart mysqld
查看主库状态
登录数据库后查看
show master status;
显示如下:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 157 | lihaozhe | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
注意:File 和 Position的值从库同步的时候需要
从库配置
在主从库中分别建一个你需要同步的测试库,比如我这里创建的库叫lihaozhe,然后在这两个库中分别建一个测试表,都是空表即可。
修改配置文件
vim /etc/my.cnf
追加以下内容:
server-id=202 #服务器 id,随意,但要唯一
log-bin=mysql-bin #二进制文件存放路径
replicate-do-db=lihaozhe #待同步的数据库
replicate-ignore-db=mysql,information_schema,performance_schema #不同步的数据库
以上面主库配置相比,就是除了server-id不一样,这里还需要配置replicate-do-db和replicate-ignore-db,注意不是日志。
重启MySQL
systemctl restart mysqld
实现主从同步
登录数据库后
-- 关闭从库
stop slave;
-- 设置同步,注意这里是主库ip,日志名称和位置是我们之前上图中看到的名称和位置
change master to master_host='192.168.1.201',master_user='lihaozhe',master_password='Lihaozhe!!@@6633',master_log_file='mysql-bin.000001',master_log_pos=157;
-- 开启从库
start slave;
-- 检查服务器状态
show slave status \G;
显示如下:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.1.201
Master_User: lihaozhe
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 472
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: lihaozhe
Replicate_Ignore_DB: mysql,information_schema,performance_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 472
Relay_Log_Space: 536
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 201
Master_UUID: ee21ecf9-01be-11ed-916c-000c29c9a6f5
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
mysql>
测试
主表操作,从表观察