mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办

首先主从同步

master 192.168.0.21

slave 192.168.0.22

#my.cnf master 配置文件
[client]
port            = 3306
socket          = /tmp/mysql.sock
default-character-set=utf8
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
default-character-set=utf8
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
server-id= 1  #增加3条配置
binlog-do-db=abc  #设置同步数据库,如果有多个数据库,每个数据库一行
binlog-ignore-db = mysql #设置不要同步的数据库,如有多个数据库,每个数据库一行
log-bin #日志文件

service mysql restart #重启数据库

进入mysql,创建一个数据库abc:
create database abc;
创建一个用来同步的用户,指定只能在192.168.0.22登录:
grant replication slave on *.* to 'root'@'192.168.0.22'identified by '123456';
mysql> use abc
Database changed
mysql> create table bb(idint, name varchar(20),age int);
Query OK, 0 rows affected (0.05 sec)
#注意表和数据库格式必须一样
#slave
mysql>  create database abc;
Query OK, 1 row affected (0.00 sec)
mysql> use abc
Database changed
mysql> create table bb(idint, name varchar(20),age int);
Query OK, 0 rows affected (0.01 sec)
#slave
#my.cnf 配置
[client]
port            = 3306
socket          = /tmp/mysql.sock
default-character-set=utf8
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
default-character-set=utf8
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
server-id= 2   #id
master-host=192.168.0.21  #mysql主ip地址
master-user=root   #授权用户
master-password=123456  #授权密码
master-port=3306    #mysql端口
master-connect-retry=30   #同步
replicate-ignore-db=mysql   #设置不要接收的数据库,如有多个数据库,每个数据库一行
replicate-do-db=abc  #设置要接收的数据库,如有多个数据库,每个数据库一行
log-bin
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
~
#主上查看
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000025 |      377 | abc          |                  |
+------------------+----------+--------------+------------------+
1 row inset(0.00 sec)
# 记录下file和position的值
#slave 上
CHANGE MASTER TO MASTER_LOG_FILE=' mysql-bin.000025',MASTER_LOG_POS=377;
# 根据master状态同步
mysql> slave start;
# 启动slave
mysql> show slave status\G;

发现报错信息

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:  mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:  mysql-bin.000025
Slave_IO_Running: No
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: 377
Relay_Log_Space: 106
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2013
Last_IO_Error: error connecting to master 'root@192.168.0.21:330                                                                                                                                                             6'- retry-time: 60  retries: 86400
Last_SQL_Errno: 0
Last_SQL_Error:
1 row inset(0.00 sec)
ERROR:
No query specified


或者vi/etc/sysconfig/iptables
-I INPUT -p tcp --dport 3360 -j DROP
-I INPUT -s 119.134.251.49/32-p tcp --dport 3306 -j ACCEPT
前面那个问题可能是防火墙问题,这次这个问题,是我以前以前同步过现在在做同步就报错了,给出解决方案。
[root@localhost ~]# service iptables stop
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:正在卸载模块:                                   [确定]
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection idis 7
Server version: 5.1.60-log Source distribution
Copyright (c) 2000, 2013, Oracle and/orits affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/orits
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;'or '\h'forhelp. Type '\c'to clearthe current input statement.
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_LOG_FILE=' mysql-bin.000025',MASTER_LOG_POS=377;
Query OK, 0 rows affected (0.05 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:  mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:  mysql-bin.000025
Slave_IO_Running: No
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: 377
Relay_Log_Space: 106
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
Last_SQL_Errno: 0
Last_SQL_Error:
1 row inset(0.00 sec)
ERROR:
No query specified


#第三个问题
[root@ceshi ~]# mysqlbinlog --start-position=377  /home/data/websrv/mysql/var/mysql-bin.000025
mysqlbinlog: unknown variable 'default-character-set=utf8'
解决:
[client]
port            = 3306
socket          = /tmp/mysql.sock
#default-character-set=utf8


[root@ceshi ~]# mysqlbinlog --start-position=377  /home/data/websrv/mysql/var/mysql-bin.000025
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#131115 22:52:33 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.60-log created 131115 22:52:33 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MTWGUg8BAAAAZgAAAGoAAAABAAQANS4xLjYwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAxNYZSEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;


看到上面 发现at4

然后我们这样配置

# at 4
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.21',MASTER_PORT=3306,MASTER_USER='root', MASTER_PASSWORD='123456',master_log_file='mysql-bin.000025',master_log_pos=4;
Query OK, 0 rows affected (0.03 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 522
Relay_Master_Log_File: mysql-bin.000025
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: 377
Relay_Log_Space: 681
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:
1 row inset(0.00 sec)
ERROR:
No query specified
好了解决了 哈哈!这时我们导入数据测试!!!
这是主数据
mysql> use abc;
Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> insert into bb VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> select* from bb;
+------+------+------+
| id| name | age  |
+------+------+------+
|    1 | 2    |    3 |
+------+------+------+
1 row inset(0.00 sec)
mysql> insert into bb VALUES(3,4,5);
Query OK, 1 row affected (0.00 sec)
mysql> select* from bb;
+------+------+------+
| id| name | age  |
+------+------+------+
|    1 | 2    |    3 |
|    3 | 4    |    5 |
+------+------+------+
2 rows inset(0.00 sec)
mysql>


slave数据:
mysql> use abc;
Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> select* from bb;
+------+------+------+
| id| name | age  |
+------+------+------+
|    1 | 2    |    3 |
+------+------+------+
1 row inset(0.00 sec)
mysql> select* from bb;
+------+------+------+
| id| name | age  |
+------+------+------+
|    1 | 2    |    3 |
|    3 | 4    |    5 |
+------+------+------+
2 rows inset(0.01 sec)


主清除数据
mysql> delete from bb;
Query OK, 2 rows affected (0.00 sec)
mysql> select* from bb;
Empty set(0.00 sec)
slave上
mysql> select* from bb;
Empty set(0.00 sec)

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办_服务器

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办_master_02

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办_master_03

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办_服务器_04


双主其实更简单

先授权

主:192.168.0.21
create database abc;
create table bb(idint, name varchar(20),age int);
grant replication slave on *.* to 'root'@'192.168.0.22'identified by '123456';
flush privileges;
主:192.168.0.22
create database abc;
create table bb(idint, name varchar(20),age int);
grant replication slave on *.* to 'root'@'192.168.0.21'identified by '123456';
flush privileges;

配置文件:

主:192.168.0.21
server-id= 1
binlog-do-db=abc
binlog-ignore-db = mysql
master-host=192.168.0.22
master-user=root
master-password=123456
master-port=3306
master-connect-retry=60
log-bin
主:192.168.0.22
server-id= 2
binlog-ignore-db = mysql
master-host=192.168.0.21
master-user=root
master-password=123456
master-port=3306
master-connect-retry=60
replicate-do-db=abc
binlog-do-db=abc
log-bin

然后重启

因为我的真实环境是主从,这里只测试了主从,双同步一样的道理,只是把主从反过来而已。

#添加开机启动项 然后开机也同步测试
[root@localhost ~]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch/var/lock/subsys/local
modprobe ip_conntrack
/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
service php-fpm start
/usr/bin/memcachedstart
service mysql restart

注意开机后防火墙端口一定要允许访问3306 否则则不同步!!!!!

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办_mysql_05