环境介绍: 主服务器: IP 192.168.1.70
从服务器: IP 192.168.1.71
主服务器配置:
修改权限:
chown -R root /usr/local/mysql51/*
chown -R mysql /usr/local/mysql51/var
chown -R mysql /usr/local/mysql51/*
修改my.cnf
vim my.cnf
server-id = 1
查看bin-log是否开启,修改之后必须重启Mysql服务
log-bin=mysql-bin
/mysql/mysql.server restart --user=root
创建一个从数据库的账户,(用于数据同步)使用REPLICATION SLAVE赋予权限,如:
mysql> grant replication slave,reload,super on *.* to "slave"@"192.168.1.71" identified by "slave";
授权登陆用户:
grant all on *.* to "xcy"@"192.168.1.70" identified by "xcy" with grant option;
grant all on *.* to"xcy"@"192.168.1.71" identified by "xcy" with grant option;
创建好了可以检查授权用户:
mysql> select Host,User,password from mysql.user;
+--------------+-----------+-------------------------------------------+
| Host | User | password |
+--------------+-----------+-------------------------------------------+
| 192.168.1.71 | slave | *51125B3597BEE0FC43E0BCBFEE002EF8641B44CF |
+--------------+-----------+-------------------------------------------+
查看master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 468 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
----------------------------------------------------------------------
从服务器配置:
修改权限:
chown -R root /usr/local/mysql51/*
chown -R mysql /usr/local/mysql51/var
chgrp -R mysql /usr/local/mysql51/*
修改my.cnf
vim my.cnf
server-id = 2
查看bin-log是否开启
log-bin=mysql-bin
授权登陆用户:>授权登陆用户:
grant all on *.* to "xcy"@"192.168.1.70" identified by "xcy" with grant option;
grant all on *.* to "xcy"@"192.168.1.71" identified by "xcy" with grant option;
重启mysql
/mysql/mysql.server restart --user=root
创建从服务器连接主服务器的连接
CHANGE MASTER TO
MASTER_HOST='192.168.1.70',
MASTER_USER='slave',
MASTER_PASSWORD='slave',
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=468;
启动从数据库的slave
mysql> slave start;
查看 是否同步;
mysql> show slave status\G
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.70
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 468
Relay_Log_File: linux-relay-bin.000002
Relay_Log_Pos: 613
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
需要注意的是 重启mysql 以后 master的 状态 会变动,创建连接的时候需要注意
如果改变了 可以修改连接文件;
change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
如果没同步成功的话
=================================================================================================================
总结:
############################## 错误处理 ① ##########################
对于 Slave_IO_Running: No
Slave_SQL_Running: Yes
错误解答
Master slave 复制错误
Description:
Slave_IO_Running:NO
Slave_SQL_Running:Yes
Seconds_Behind_Master: NULL
本人遇到的Slave_IO_Running:NO的情况有下面两种:
1. 在配置slave同步时因为slave访问master没有权限导致;
2. master上的mysql-bin.xxxxxx文件全被我误删除了;
对于第一种情况,仔细检查数据库访问权限即可解决;
对于第二种情况:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.123
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 173
Relay_Log_File: mysqld-relay-bin.000008
Relay_Log_Pos: 98
Relay_Master_Log_File: mysql-bin.000016
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table: br>
Last_Error: 0
Skip_Counter: 0
Exec_Master_Log_Pos: 173
Relay_Log_Space: 98
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: NULL
1 row in set (0.00 sec)
解决步骤:
重启master库:service mysqld restart
mysql> show master status;
+------------------+--------------------+----------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-------------------+-----------------+------------------+
| mysql-bin.000001 | 98 | | |
+------------------+--------------------+-----------------+------------------+
mysql> slave stop;
mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
mysql> slave start;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.123
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
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: 235
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
1 row in set (0.00 sec)
############################## 错误处理 ② ##########################
问题:如果你的mysql数据库已经有数据存储使用,将导致master与slave日志错误!
mysql> start slave;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
mysql> show slave status;
Empty set (0.00 sec)
处理:删除mysql-bin.000001 日志文件,并重启mysql服务
1、master
#rm -rf /var/lib/mysql/*.*
2、slave
#rm -rf /usr/local/mysql/var/*.*
3、service mysqld restart