主从复制的搭建
#
做主从的前提
- 两台服务器的防火墙都开放了各自 mysql 的服务端口(下面以默认的 3306 为例子)
- 从库无法同步主库之前的数据。如果主库之前有数据,那么先把主库的数据导入到从库中。保证两台服务器在做主从复制之前的数据一致性
- 尽量保证两台服务器的 my.cnf 文件只有 server-id 不同。其他的配置都相同
修改主服务器的配置
vi /etc/my.cnf
[mysqld]
启用主从配置(主服务器)
主服务器id
server-id=1
二进制日志
log-bin=mysqlbin
设置忽略复制的数据库
binlog-ignore-db=mysql
设置需要复制的数据库
binlog-do-db=dtjc重启 mysql 服务器
service mysqld restart
mysqld 无效的话把 mysqld 换成 mysql
运行
mysql> show master status;
+-----------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+------------------------------------------+
| mysqlbin.000003 | 883 | | | e730104b-113f-11eb-9739-000c2972b171:1-7 |
+-----------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.01 sec)后续需要使用file和position这两个字段
为从服务器生成专门的账号用来做主从复制,同时赋予做从服务器的权限
create user 'repl'@'%' identified by '123456Aa?';
grant replication slave,replication client on *.* to 'repl'@'%';
flush privileges;修改从服务器的配置
vi /etc/my.cnf
[mysqld]
启用主从配置(主服务器)
从服务器id
server-id=2
二进制日志
log-bin=mysqlbin
设置忽略复制的数据库
binlog-ignore-db=mysql
设置需要复制的数据库
binlog-do-db=dtjc重启 mysql 服务
service mysqld restart
mysqld 无效的话把 mysqld 换成 mysql
slave 节点测试 repl 用户远程连接 mater 节点
mysql -h192.168.220.10 -P3306 -urepl -p123456Aa?
链接成功,即可进行下一步,否则要排错
退出 master 节点的登陆,登陆本机的 mysql,运行以下命令
change master to master_host='192.168.220.10',master_port=3306,master_user='repl',master_password='12345Aa?',master_log_file='mysqlbin.000003',master_log_pos=883;
master_log_file 就是主服务器的 file 字段,883 就是主服务器的 position 字段
如果第 8 步的 mysql 没有报错的话,查看 slave 状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.220.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlbin.000003
Read_Master_Log_Pos: 196
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 323
Relay_Master_Log_File: mysqlbin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 196
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: 1
Master_UUID: e730104b-113f-11eb-9739-000c2972b171
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: e730104b-113f-11eb-9739-000c2972b171:1-3
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)如果
Slave_IO_Running: Yes
Slave_SQL_Running: Yes那么主从配置就搭建好了
如果 Slave_IO_Running 或者 Slave_SQL_Running 有任意一个不是 Yes 的话,搭建失败
运行以下命令,停止主从。然后从最开始一步步排错
stop slave;
reset slave all;
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment