[7] MySQL Master-Slaves master-slave configuration

1 Preparation

Refer to the previous blog post, you should have installed MySQL and copied a new virtual machine,

主库地址: 192.168.56.101 
主库地址: 192.168.56.102

2 Main library configuration

2.1 Create a synchronization account on the master

# 赋予账号 repl replication slave 权限, 其它 slave 服务器可以通过这个账号来访问 master
grant replication slave on *.* to 'repl'@'192.168.56.102' identified by
'your password';

Note that the IP address here is slave.

2.2 Configure the master node, open binlog
vim /etc/my.cfg
configuration as follows: open binlog, set node id:

log-bin=mysql-bin # 日志文件名前缀
server-id=1 # 在集群中的唯一id,值可以为1~2^32-1的整数

2.3 Restart the mysql service

systemctl restart mysqld

2.4 View the status of the main library

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

3 Configuration from the library

3.1 Configure slave server-id

# vim /etc/my.cfg
server-id=2 # 在集群中的唯一id,值可以为1~2^32-1的整数

Restart the mysql service:

systemctl restart mysqld

3.2 Execute the CHANGE MASTER TO statement to set the synchronized main library

CHANGE MASTER TO
  MASTER_HOST='192.168.56.101',  # master 的 ip
  MASTER_USER='repl',
  MASTER_PASSWORD='your password';


3.3 Open the slave library set here

START SLAVE;

View slave status:

SHOW SLAVE STATUS;

4 Problems with replication

Note] Generated uuid: '5348c44b-c0a4-11e9-bb3d-0800275e32c6', server_start_time: 1111544684596399254, bytes_sent: 71674160
2019-08-17T04:06:01.595

This is when my virtual machine is cloned so that the mysql directories of master and slave are the same, fix:

cd /var/lib/mysql
mv auto.cnf auto.cnf.bac
systemctl restart mysqld  # 重启 mysql

You can check the /var/log/mysqld.loglog file to find problems in mysql running.

This step should be fine. Now create a new database and table in the master, and the slave can see them synchronously.

Reference: Summary of master-slave replication errors

Guess you like

Origin blog.csdn.net/jiaobuchong/article/details/99690854
Recommended