mysql主从复制搭建 (对可能出现的问题案例解答)
(1)创建一个用于主从复制的用户
create user `slave`@`%` identified by "root"; grant all privileges on *.* to 'slave'@'%'; --给用户权限
(2)是否开启bin-log
vi /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1
(3)查看当前bin-log
show master status;
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 10820 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
(4)从库配置主从信息
停止从库 stop slave ;
配置从库 (不要换行,不要换行,不要换行 ,用于同步的用户的用户名和密码不要错)
change master to master_host='192.168.8.110',master_port=3306,master_user='slave',master_password='root',master_log_file='mysql-bin.000002',master_log_pos=10820;
> 启动从库 start slave;
(5)查看从库状态
show slave status \G;
(5)看到2个Yes 主从搭建成功
Slave_IO_Running: Yes Slave_SQL_Running: Yes
Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 2003 Last_IO_Error: error connecting to master 'slave@192.168.8.112:3306' - retry-time: 60 retries: 20 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: Slave has read all relay log; waiting for more updates
出现以下错误 出现以下错误
(1)可能你的配置错误
master_host,
master_port,
master_user,
master_password
master_log_file
master_log_pos
change master to
(2)从库的ip ping 主库的ip
(3)在从库 mysql -uslave -proot -h192.168.8.112是否可以连接(3)在从库 mysql -uslave -proot -h192.168.8.112是否可以连接
可能存在的问题
从库的service_id 和主库一样了
vi /etc/my.cnf
修改在从新启动mysql
service mysql restart;
开始主从2库最好一致,避免其他不同步(采用热备份)
开始主从2库最好一致,避免其他不同步(采用热备份)
开始主从2库最好一致,避免其他不同步(采用热备份)
测试结果
同步库
同步表
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: