前提条件:
准备两台server,安装好相同版本的mysql.
操作系统版本:
[root@master ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
Mysql版本:
[root@mysql ~]# mysql -Version
mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
步骤:
1. 配置master
vi /opt/mysql/conf/my.cnf,在[mysqld] 下新加:
log_bin=/var/log/mysql/mysql-bin
server_id=164 #此处serverid可以随便设置,但不能与slave冲突
#binlog-do-db=
#binlog-ignore-db=
2. 配置slave
vi /opt/mysql/conf/my.cnf,在[mysqld] 下新加:
log_bin=/var/log/mysql/mysql-bin
server_id=167 #此处serverid可以随便设置,但不能与master冲突
relay_log=/var/log/mysql/relay-bin
read_only=on
3. 在master上创建用户复制的数据库账号并授权
mysql> create user rep@10.224.12.167 identified by ‘pass’;
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on . to rep@10.224.12.167 ;
Query OK, 0 rows affected (0.00 sec)
这里10.224.12.167为slave server的ip地址,表明这个user只能从slave server上登录,为复制专用。
4. 重启master db
service mysql restart
CentOS7 下也可以用:
systemctl restart mysql
5. 查看master 状态
mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000001 | 154 | | | |
±-----------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
6. 备份master服务器上的数据并初始化slave服务器数据(跳过)
mysqldump --master-data=2 -uroot -p -A --single-transaction -R --triggers
由于我的slave是从master clone过来的,所以跳过此步。
7. 删除slave server的 auto.cnf
MySQL5.7增加了server-uuid值,默认情况下在auto.cnf文件中,因为我的slave是从mastr clone过来的,所以slave和master server上的uuid一样 ,所以要把auto.cnf文件删除掉。MySQL重启后会自动重新生成uuid的值,这样就可以保证不同服务器上的MySQL实例的uuid的值是不一样的;
[root@master conf]# rm -rf /var/lib/mysql/auto.cnf
8. 重启slave db
service mysql restart
9. 配置slave 基于日志点的复制链路
mysql> change master to
-> master_host=‘10.224.12.164’,
-> master_user=‘rep’,
-> master_password=‘pass’,
-> MASTER_LOG_FILE=‘mysql-bin.000001’,
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 1 warning (0.14 sec)
备注:MASTER_LOG_FILE 和 MASTER_LOG_POS 来自master db上 show master status的结果
mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000001 | 154 | | | |
±-----------------±---------±-------------±-----------------±------------------+
10. 查看slave 状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 10.224.12.164
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 154
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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:
1 row in set (0.00 sec)
ERROR:
No query specified
备注: 此时发现状态不对,复制不work, Slave_IO_Running: No 和 Slave_SQL_Running: No
解决:重启slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.224.12.164
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
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: 154
Relay_Log_Space: 521
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: 164
Master_UUID: 295c5344-7fa5-11ea-ae77-005056921132
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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:
1 row in set (0.00 sec)
ERROR:
No query specified
11. 验证replication
1)在master db insert一条记录
mysql> use test;
mysql> select * from tmp1;
±-----±------+
| id | name |
±-----±------+
| 1 | test1 |
±-----±------+
1 row in set (0.00 sec)
mysql> insert into tmp1(id,name) values(2, ‘test2’);
Query OK, 1 row affected (0.08 sec)
2)在slave db query
master insert 之前:
mysql> select * from tmp1;
±-----±------+
| id | name |
±-----±------+
| 1 | test1 |
±-----±------+
1 row in set (0.02 sec)
master insert 之后:
mysql> select * from tmp1;
±-----±------+
| id | name |
±-----±------+
| 1 | test1 |
| 2 | test2 |
±-----±------+
删除主从信息:
1. stop slave
mysql> stop slave;
Query OK, 0 rows affected (0.04 sec)
2. reset slave
mysql> reset slave;
Query OK, 0 rows affected (0.08 sec)
3. 复制链路重置
mysql> change master to master_user=’’, master_host=’ ‘, master_password=’’;
Query OK, 0 rows affected, 1 warning (0.08 sec)