cd /data/
mv mysql mysqlback
本人做了mysql的主主同步, 因为不慎将数据库连接误操作,两台数据库同时写入同一条数据 ,导致同步挂掉
没办法 只能重新做
思路:
1:停掉所以关于MYSQL的数据连接 ,锁表 不产生新的数据
2:将一台的mysql所有数据拷到另外一台
3:重新加载master salve 就可以了。
下面是两台mysql服务器 A 和 B
A:/etc/mysql/my.cnf
[mysqld] =======================这个节点下面加下面的。 做过的。应该已经加过了。
server-id=1 #节点标识,主从节点要唯一
log-bin=mysql-bin #开启mysql的二进制的日志功能 会生成mysql-bin.000001/000002
relay-log=mysql-relay-bin #用来定义中继日志文件的命名格式
replicate-wild-ignore-table=mysql.% #不复制mysql底下的任何表
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
replicate-wild-ignore-table=syslog.%
replicate-wild-ignore-table=iis.%
B:/etc/mysql/my.cnf
[mysqld] =======================这个节点下面加下面的。 做过的。应该已经加过了。
server-id=2 #节点标识,主从节点要唯一
log-bin=mysql-bin #开启mysql的二进制的日志功能 会生成mysql-bin.000001/000002
relay-log=mysql-relay-bin #用来定义中继日志文件的命名格式
replicate-wild-ignore-table=mysql.% #不复制mysql底下的任何表
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
replicate-wild-ignore-table=syslog.%
replicate-wild-ignore-table=iis.%
A 和 B
停掉连mysql的所有服务 不让产生新数据
service mysql stop
service zabbix-server stop
service rsyslog stop
A 备份mysql的文件夹所有数据
cd /data/
tar zcvf mysql.tar.gz mysql
B 备份mysql的文件夹 把A的mysql数据拿过来替换一下
cd /data/
mv mysql mysqlback
A备份好以后 scp方式传到B服务器data目录
scp mysql.tar.gz ubuntu@B的IP:/tmp
B /tmp目录移到/data目录
cp /tmp/mysql.tar.gz /data/
cd /data/
tar -zxvf mysql.tar.gz
A 启动mysql 锁表 停止slave 禁止新数据产生
service mysql start
mysql -uroot -p
stop slave;
FLUSH TABLES WITH READ LOCK;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000476 | 2271 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
B 启动服务 。停址slave 还要看主主同步用户名。 更改登录IP
service mysql restart
mysql -uroot -p
mysql> update mysql.user set host='172.31.32.3' where user='repl_user';
mysql> flush privileges;
stop slave;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000476 | 358 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
A 指定master 启动slave 解锁表
unlock tables;
change master to master_host='172.31.32.4',master_user='repl_user',master_password='zabbix',master_port=3306,master_log_file='mysql-bin.000476',master_log_pos=358;
start slave;
B指定master 启动slave
change master to master_host='172.31.32.3',master_user='repl_user',master_password='zabbix',master_port=3306,master_log_file='mysql-bin.000476',master_log_pos=2271;
start slave;
最终的效果
A: show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.31.32.4
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000476
Read_Master_Log_Pos: 433
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 328
Relay_Master_Log_File: mysql-bin.000476
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: mysql.%,test.%,information_schema.%,syslog.%,iis.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 433
Relay_Log_Space: 484
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
B:mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.31.32.3
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000476
Read_Master_Log_Pos: 25005
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 22987
Relay_Master_Log_File: mysql-bin.000476
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: mysql.%,test.%,information_schema.%,syslog.%,iis.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 25005
Relay_Log_Space: 23143
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这两个为yes就OK啦
如果为NO 那么这里面也会提示错误信息, 一一解决就可以了。