mysq的主从复制、备份、还原
搭建实验环境说明:
1、授权从服务器的root用户来主服务器同步资源的权限
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO root#slave IP IDENTIFIED BY '123456'; //将#换成@server-id=1
log-bin=mysql-bin
binlog-do-db=mytest
binlog-ignore-db=mysql
#binlog-do-db=//需要备份的数据库名,可写多行
#binlog-ignore-db= // 不需要备份的数据库名,可写多行server-id=2
log-bin=mysql-bin
master-host=192.168.0.123
master-user=rootmaster-port=3306
replicate-do-db=mytest
可选项如下:
#replicate-do-db=test//需要备份的数据库名
#replicate-ignore-db=mysql//忽略的数据库
# master-connect-retry=60 //如果从服务器发现主服务器断掉,重新连接的时间差(秒)mysql>showdatabases;
mysql> use mytest;|Tables_in_mytest|
+------------------+
|mytable|
+------------------+
1 row in set (0.01 sec)|name|sex|birth|birthaddr|
+----------+------+------------+-----------+
|hanfeng|s|1985-08-08|china|
|feiniao|g|1988-08-08|china|
+----------+------+------------+-----------+|Tables_in_mytest|
+------------------+
|mytable|
+------------------+|name|sex|birth|birthaddr|
+----------+------+------------+-----------+
|hanfeng|s|1985-08-08|china|
|feiniao|g|1988-08-08|china|
+----------+------+------------+-----------+mysql>showslavestatus\G;
***************************1.row*******************
Slave_IO_State:Waitingformastertosendevent必须的
Master_Host:192.168.0.123
Master_User: rootConnect_Retry:60
Master_Log_File:mysql-bin.000002
Read_Master_Log_Pos:287
Relay_Log_File:mysqld-relay-bin.000009
Relay_Log_Pos:424
Relay_Master_Log_File:mysql-bin.000002
Slave_IO_Running:Yes#为YES状态(说明:如果出现NO请参看下面错误解答!)
Slave_SQL_Running:Yes#为YES状态
Replicate_Do_DB:mytest
Replicate_Ignore_DB:##########################在mastermysql操作:##########################
mysql>usemytest;
mysql>insert into mytable values ('shanghai','s','2011-09-13','china');向mytest数据库的mytable表中插入一新记录;
在slave服务器上
mysql>usemytest;
mysql>select*frommytable;
得到:
mysql>select*frommytable;
+-----------+------+------------+---------------+
|name|sex|birth|birthaddr|
+-----------+------+------------+----------------+
|hanfeng|b|1987-08-08|china|
|feiniao|g|1988-08-08|china|
|shanghai|s|2011-09-13|china|
+-----------+------+------------+---------------+
3 rows in set (0.01 sec)Slave_SQL_Running:Yes
错误解答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>showslavestatus\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>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库:servicemysqldrestart
mysql>showmasterstatus;
+------------------+--------------------+----------------+------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|
+------------------+-------------------+-----------------+------------------+
|mysql-bin.000001|98|||
+------------------+--------------------+-----------------+------------------+
mysql>slavestop;
mysql>changemastertoMaster_Log_File='mysql-bin.000001',Master_Log_Pos=98;
mysql>slavestart;
mysql>showslavestatus\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
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
1rowinset(0.00sec)
mysql>startslave;
ERROR1201(HY000):Couldnotinitializemasterinfostructure;moreerrormessagescanbefoundintheMySQLerrorlog
mysql>showslavestatus;
Empty set (0.00 sec)1、master
#rm-rf/var/lib/mysql/*.*
2、slave
#rm-rf/usr/local/mysql/var/*.*
3、servicemysqldrestart