如果主库已经投入使用了,此时如果需要主从同步,需要导出所表结构和数据,然后导入到从库中。这时需要注意修改/var/lib/mysql/auto.cnf中uuid。
master配置
0、修改配置文件 vim /etc/my.cnf
[mysqld]
# 日志文件名
log-bin = mysql-bin
# 要同步的数据库
binlog-do-db = my_db_name
# 主数据库端ID号
server-id = 1
# 跳过checksum的事件
binlog_checksum = none
systemctl restart mysqd.service重启。
1、创建一个专门为复制服务的用户
create user 'repl'@'%' identified by '123456';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
mysql8.0中默认使用caching_sha2_password插件,由于一些原因,可能我们配置的使用mysql_native_password。
create user 'repl'@'%' identified with mysql_native_password by '123456';
2、查看master 状态
flush tables with read lock;
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000013 | 150 | octopus | | |
+------------------+----------+--------------+------------------+-------------------+
slave端配置
1、配置文件 vim /etc/my.cnf
[mysqld]
# 主数据库ID号
server-id = 2
slave_sql_verify_checksum = NONE
replicate-do-db = my_db_name
#replicate-rewrite-db = my_db_name -> new_db_name
重启。
2、执行同步
change master to master_host=‘120.77.xx.xx’,master_user=‘repl’,master_password=‘111111’,master_log_file=‘mysql-bin.000013’,master_log_pos=150;
start slave;
3、查看同步状态
show slave status\G;
需要slave_io_running和slave_sql_running都是yes,表示成功。slave_sql_running负责与主机的io通信,slave_io_running负责自己的slave mysql进程。
slave_io_running: no的原因:
(1)mysql有个uuid,然而uuid是唯一的,在物理备份时,可能没有改,需要修改/var/lib/mysql/auto.cnf中的uuid。
(2)server-id是否唯一。