本篇内容主要讲解“Mysql搭建主从复制的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mysql搭建主从复制的方法”吧!
主库:192.168.1.1
从库:192.168.1.2
数据文件存放位置:/data/datafile
待同步数据库:repdb
一.设置MASTER
1.修改my.cnf
# vi /etc/my.cnf
server-id = 1
log-bin
binlog-ignore-db=mysql
2.赋予SLAVE权限帐号,允许用户在SLAVE上LOAD TABLE和LOAD DATA
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO user_rep@192.168.1.2 IDENTIFIED BY 'rep123';
3.锁主库表
mysql>FLUSH TABLES WITH READ LOCK;
4.显示主库信息
记录File和Position,从库设置将会用到
mysql>SHOW MASTER STATUS G;
*************************** 1. row ***************************
File: mysql-bin.000030
Position: 391156558
Binlog_Do_DB:
Binlog_Ignore_DB:
5.另开一个终端,打包主库
/data/datafile/ # tar cvf repdb.tar repdb
二.设置SLAVE
1.传输拿到主库包,解包
/data/datafile/ # scp 192.168.1.1:/data/datafile/repdb.tar .
/data/datafile # tar xvf repdb.tar
2.解锁主库表
mysql>UNLOCK TABLES;
3.查看修改repdb文件夹权限
/data/datafile # chown mysql:mysql repdb -R
4.修改my.cnf
# vi /etc/my.cnf
#slave
server-id=2
master-host=192.168.1.1
master-user=user_rep
master-password=rep123
master-port=3306
replicate-do-db=repdb
5.验证连接MASTER
# mysql -h292.168.1.1 -uuser_rep -prep123
mysql>show grants;
+------------------------------------------------------------------------------+
| Grants for user_rep@192.168.1.2 |
+------------------------------------------------------------------------------+
| GRANT SELECT, FILE, REPLICATION SLAVE ON *.* TO user_rep@192.168.1.2 IDENTIFIED BY
PASSWORD '*9FF2C222F44C7BBA5CC7E3BE8573AA4E1776278C' |
+------------------------------------------------------------------------------+
6.本地登录mysql
设置连接MASTER MASTER_LOG_FILE为主库的File
MASTER_LOG_POS为主库的Position
mysql>CHANGE MASTER TO
MASTER_HOST='192.168.1.1',
MASTER_USER='user_rep',
MASTER_PASSWORD='rep123',
MASTER_LOG_FILE='mysql-bin.000030',
MASTER_LOG_POS=391156558;
7.启动SLAVE服务
mysql>slave start;
8.本地登录mysql,查看SLAVE状态
mysql>SHOW SLAVE STATUS G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: user_rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 98
Relay_Log_File: relay.000003
Relay_Log_Pos: 232
Relay_Master_Log_File: binlog.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: 98
Relay_Log_Space: 232
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
Slave_IO_Running 和Slave_SQL_Running 两列的值都为 "Yes",表明 Slave 的 I/O 和 SQL 线程都在正常运行.
到此主从库搭建成功. [@more@]
从库SLAVE启动问题
由于一些错误操作导致CHANGE MASTER和SLAVE服务无法启动,系统报错如下:
Could not initialize master info structure; more error messages can be found in the MySQL error log.
无法初始化master info结构;MySQL错误日志记录了更详细的错误信息.
解决方法:
1.查看MySQL错误日志,查看原因.
如:同步的上一个Position是多少.
很多情况下无法启动服务是由于mysql识别的同步始终停留在上一个Position上.
2.查看master.info和relay-log.info
master.info 记录MASTER相关信息
14
mysql-bin.000030
391156558
192.168.1.1
user_rep
rep123
3306
60
0
relay-log.info 记录当前同步日志信息
235
mysql-bin.000030
391156558
3.停止myslq服务,删除master.info和relay-log.info
# service mysql stop
/data/datafile/ # rm master.info
/data/datafile/ # rm relay-log.info
4.启动mysql服务
# service mysql start
5.重新CHANGE MASTER,重新启动SLAVE服务.
问题应该就可以解决了.
到此,相信大家对“Mysql搭建主从复制的方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!