MYSQL 的主主同步配置及问题

刚刚抽空做了一下MYSQL 的主主同步。
把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST
1、环境描述。
主机:192.168.0.231(A)
主机:192.168.0.232(B)
MYSQL 版本为5.1.21
2、授权用户。
A:
mysql> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'repl2'@'192.168.0.231' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。


3、配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates                             #如果一个MASTER 挂掉的话,另外一个马上接管。
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1


B:
user = mysql
log-bin=mysql-bin
server-id       = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates               
slave-skip-errors=all
sync_binlog=1 #服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,
auto_increment_increment=2 #日志刷新到另外一台。从而保证了数据的同步 。
auto_increment_offset=2 #


对上面参数作出部分解释:
 
log-bin=mysql-bin                   #M/S 需开启log-bin 日记文件
server-id=1                                #指定server-id 必须不一致,M/s 结构时 M > S
binlog-do-db=test                           #同步数据库名称
binlog-ignore-db=mysql                     #忽略数据名称
replicate-do-db=test                       #用于控制slave来执行同步的行为
replicate-ignore-db=mysql                  #用于控制slave来执行同步的行为
log-slave-updates                          #把更新的记录写到二进制文件中
slave-skip-errors=all                      #跳过错误,继续执行复制
auto_increment_increment=2                 #设置主键单次增量
auto_increment_offset=1                    #设置单次增量中主键的偏移量
#expire_logs_days = 20                     #设置log-bin 超过多少天删除
max-binlog-size= 512M
 
# auto_increment_increment、auto_increment_offset 可以防止双主主键冲突问题


4、重新启动MYSQL服务器。
在A和B上执行相同的步骤
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
[1] 4264
[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
/usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected
071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data


5、进入MYSQL的SHELL。
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)


mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)


B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)


mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
    -> master_host='192.168.0.232',
    -> master_user='repl2',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)




B:
mysql> change master to
    -> master_host='192.168.0.231',
    -> master_user='repl1',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000007',
    -> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)


7、查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:


mysql> show processlist\G
*************************** 1. row ***************************
Id: 2
User: repl
Host: 192.168.0.232:54475
db: NULL
Command: Binlog Dump
Time: 1590
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 2. row ***************************
Id: 3
User: system user
Host: 
db: NULL
Command: Connect
Time: 1350
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 4
User: system user
Host: 
db: NULL
Command: Connect
Time: 1149
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 4. row ***************************
Id: 5
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
4 rows in set (0.00 sec)


B:


mysql> show processlist\G
*************************** 1. row ***************************
Id: 1
User: system user
Host: 
db: NULL
Command: Connect
Time: 2130
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host: 
db: NULL
Command: Connect
Time: 1223
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 4. row ***************************
Id: 5
User: repl2
Host: 192.168.0.231:50718
db: NULL
Command: Binlog Dump
Time: 1398
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
4 rows in set (0.00 sec)


如果红色部分没有出现,检查DATA目录下的错误文件。


8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)


插入之前两个机器表的对比:
A:


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     | 
| t22            | 
+----------------+
B:


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     | 
| t22            | 
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
    -> (id int not null auto_increment primary key,
    -> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.01 sec)


mysql> insert into t11_replicas(str) values
    -> ('This is a master to master test table');
Query OK, 1 row affected (0.01 sec)


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     | 
| t11_replicas   | 
| t22            | 
+----------------+
3 rows in set (0.00 sec)


mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table | 
+----+---------------------------------------+
1 row in set (0.00 sec)




现在来看B机器:


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     | 
| t11_replicas   | 
| t22            | 
+----------------+
3 rows in set (0.00 sec)


mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table | 
+----+---------------------------------------+
1 row in set (0.00 sec)


现在反过来从B机器上插入数据:
B:


mysql> insert into t11_replicas(str) values('This is a test 2');
Query OK, 1 row affected (0.00 sec)


mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table | 
|  2 | This is a test 2                      | 
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table | 
|  2 | This is a test 2                      | 
+----+---------------------------------------+
2 rows in set (0.00 sec)


好了。现在两个表互相为MASTER。


对开启权限、grant 这些基本的这里就不在详细说明,下面贴出自动建立同步的gant 脚本,项目在生产过程中总会遇到Mysql 数据库服务器宕机等情况,可用以下脚本来重新构建Master -to-Master 环境。


#!/bin/bash 
# Setting Variables
 _REMOTEHOST=192.168.0.231  #远程主机IP
 _LOCALHOST=192.168.0.232   #本地主机IP
 _USER=root                #用户名
 _REMOTEPASD=123456        #远程主机密码
 _LOCALPASD=123456         #本地主机密码
 _BASE=TSC
 
 _LF=`mysql -u root -h $_REMOTEHOST -p$_REMOTEPASD -e "show master status\G;" | awk '/File/ {print $2}'`
 _LLF=`mysql -u root -p$_LOCALPASD -e "show master status\G;" | awk '/File/ {print $2}'`
 _PS=`mysql -u root -h $_REMOTEHOST -p$_REMOTEPASD -e "show master status\G;" | awk '/Position/ {print $2}'`
 _LPS=`mysql -u root -p$_LOCALPASD -e "show master status\G;" | awk '/Position/ {print $2}'`
 
# Backup Mysql 
mysqldump -u root -h $_REMOTEHOST -p$_REMOTEPASD  $_BASE > $_BASE.sql
mysql -u root -p$_LOCALPASD $_BASE < $_BASE.sql
rm -rf $_BASE.sql
 
mysql -uroot -p$_LOCALPASD -e "stop slave;"
mysql -h $_REMOTEHOST -uroot -p$_LOCALPASD -e "stop slave;"
 
echo "mysql -uroot -p$_LOCALPASD -e +change master to master_REMOTEHOST=*${_REMOTEHOST}*,master_user=*${_USER}*,master_password=*${_REMOTEPASD}*,master_log_file=*${_LF}*,master_log_pos=${_PS};+" > tmp
echo "mysql -h $_REMOTEHOST -uroot -p$_LOCALPASD -e +change master to master_REMOTEHOST=*${_LOCALHOST}*,master_user=*${_USER}*,master_password=*${_LOCALPASD}*,master_log_file=*${_LLF}*,master_log_pos=${_LPS};+" > tmp2
 
 
  sed -ri 's/\+/"/g' tmp
  sed -ri 's/\+/"/g' tmp2
  sed -ri "s/\*/\'/g" tmp
  sed -ri "s/\*/\'/g" tmp2
  sh tmp
  sh tmp2
  rm -rf tmp
  rm -rf tmp2
 
mysql -uroot -p$_LOCALPASD -e "start slave;"
mysql -h $_REMOTEHOST -uroot -p$_LOCALPASD -e "start slave;"
 
mysql -uroot -p$_LOCALPASD -e "show slave status\G;" | awk '$0 ~/Host/ || $0 ~/State/'
mysql -h $_REMOTEHOST -uroot -p$_LOCALPASD -e "show slave status\G;" | awk '$0 ~/Host/ || $0 ~/State/'




这个过程可能会出现的问题:
一、出现ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.231'(111)错误。
1、可能网络连接问,远程ping 192.168.0.231 ,能ping通,排除此情况
 
[mysql@vvmvcs0 ~]$ ping 192.168.0.231 
PING 192.168.0.231 (192.168.0.231) 56(84) bytes of data.
64 bytes from 192.168.0.231: icmp_seq=1 ttl=63 time=0.230 ms
 
2,排查可能由于85上my.cnf里配置了skip_networking或者bind_address,只允许本地socket连接
2.1 在[mysqld]下设置skip_networking,
知识说明: 这使用MySQL只能通过本机Socket连接(socket连接也是本地连接的默认方式),放弃对TCP/IP的监听  www.2cto.com  
当然也不让本地java程序连接MySQL(Connector/J只能通过TCP/IP来连接)。
2.2 可能使用了bind_address=127.0.0.1(当然也可以是其他ip)
 
[mysqld] 
bind_address=127.0.0.1
知识说明:这种情况可以TCP/IP连接
通过查看了my.cnf文件,以上两个都是没设置的,排除掉这两种情况
 
3,排查DNS解析问题,检查是否设置了: skip_name_resolve。 这个情况肯定不可能,因为我用的是ip,不是主机名。
 
[mysqld]
skip_name_resolve
知识说明:这个参数加上后,不支持主机名的连接方式。
 
4, 排查用户和密码问题, 其实用户和密码的错误,不会出现111的,所以排除用户密码问题
ERROR 1045 (28000): Access denied for user 'root'@'192.168.0.231' (using password: YES)
 
5,排查--port问题,有可能85的MySQL port不是默认3306, 这样我远程连接时,没有指定--port,用的是3306, 而85上没有对3306进行监听。
ps -ef | grep mysqld
果然是: 85上的MySQL使用的是3308 port.
最终连接方式:加上--port=3308


6、如果都不是,那么可能是防火墙的问题,可以把iptables 服务关闭
service iptables stop


除此之外,可以从日志文件中来获取错误信息。我的linux里面是放在/var/log/mysqld.log中
[mysql@vvmvcs0 ~]$ tail -n100 /var/log/mysqld.log
150430  9:41:06 [Note] /usr/libexec/mysqld: Normal shutdown


150430  9:41:06 [Note] Event Scheduler: Purging the queue. 0 events
150430  9:41:06 [Note] Error reading relay log event: slave SQL thread was killed
150430  9:41:06 [Note] Slave I/O thread killed while connecting to master
150430  9:41:06 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000002', position 1360
150430  9:41:06  InnoDB: Starting shutdown...
150430  9:41:09  InnoDB: Shutdown completed; log sequence number 0 44233
150430  9:41:09 [Note] /usr/libexec/mysqld: Shutdown complete
50430  9:43:09 [ERROR] Slave I/O: error connecting to master 'repl1@192.168.0.232:3306' - retry-time: 60  retries: 86400, Error_code: 2013
150430  9:54:00 [Note] Error reading relay log event: slave SQL thread was killed
150430  9:54:00 [Note] Slave I/O thread killed while connecting to master
150430  9:54:00 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000002', position 1360


150430  9:55:42 [ERROR] Slave I/O: error connecting to master 'linxw@10.0.0.27:3306' - retry-time: 60  retries: 86400, Error_code: 2013
150430  9:55:42 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000003' at position 106, relay log './mysqld-relay-bin.000001' position: 4


二、Access denied; you need the SUPER privilege for...
运行脚步的时候可能会有这个错误,那么需要赋权限:
ysql> grant super on *.* to root@192.168.0.232 identified by 'engine';
Query OK, 0 rows affected (0.02 sec)


mysql> flush privilege;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'privilege' at line 1
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值