mysql5.7
服务器信息如下(实验环境,关闭机器的iptables防火墙和selinux):
node1 | 10.1.6.251 | 物理数据库Master数据库 |
node2 | 10.1.6.252 | 物理数据库Slave数据库 |
os | ubuntu18.04 |
在Master服务器上,增加一个用户账号(mysqlsync)作为同步用户账号.
mysql> GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
刷新授权,不然授权在MySQL重启前不生效,执行这条指令后,即刻生效
flush privileges;
修改mysqld.cnf
libo@node1:/etc/mysql/mysql.conf.d$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf;
更改如下内容:
server-id = 251
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
binlog_do_db = asd #asd是需要同步的数据库名称
重启mysql服务:
libo@node1:/etc/mysql/mysql.conf.d$ systemctl restart mysql;
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: libo
Password:
==== AUTHENTICATION COMPLETE ===
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
#把数据表先锁住,不让Position 变化!
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | asd | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
slave端:
libo@node2:/etc/mysql/mysql.conf.d$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf;
server-id = 252
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
binlog_do_db = asd
slave端重启mysql
systemctl restart mysql
在mysql控制台下输入匹配主从模式的命令
mysql> change master to \
-> master_host="10.1.6.251", \
-> master_user="root", \
-> master_password="123456", \
-> master_log_file="mysql-bin.000001", \
-> master_log_pos=154;
ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
mysql> show variables like '%repository%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| master_info_repository | FILE |
| relay_log_info_repository | FILE |
+---------------------------+-------+
2 rows in set (0.00 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> drop table slave_master_info;
Query OK, 0 rows affected (0.04 sec)
mysql> drop table slave_relay_log_info;
Query OK, 0 rows affected (0.08 sec)
mysql> drop table slave_worker_info;
Query OK, 0 rows affected (0.03 sec)
mysql> drop table innodb_index_stats;
Query OK, 0 rows affected (0.06 sec)
mysql> drop table innodb_table_stats;
Query OK, 0 rows affected (0.05 sec)
slave端:
重新启动mysql服务
root@node2:~# systemctl restart mysql
重新配置在mysql控制台下输入匹配主从模式的命令
mysql> change master to \
-> master_host="10.1.6.251", \
-> master_user="root", \
-> master_password="123456", \
-> master_log_file="mysql-bin.000001", \
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.27 sec)
mysql> show slave status\G