MySQL 5.5 主主复制搭建过程是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
–节点1 IP 192.168.78.141 PORT 3306
–节点2 IP 192.168.78.137 PORT 5505
–配置节点1到节点2的复制
–编辑节点1的配置文件
[root@localhost install]# vim /etc/my.cnf
# Log
server-id = 100
log-bin = /log/binlog/mysql-bin
–在节点2安装好MySQL软件,安装流程可以参考源码安装文章
http://blog.itpub.net/26506993/viewspace-2072859/
–在节点1,使用Xtrabackup创建完整备份
关于Xtrabackup,可参考
http://blog.itpub.net/26506993/viewspace-2087734/
http://blog.itpub.net/26506993/viewspace-2088737/
[root@localhost backup]# /install/percona/percona-xtrabackup-2.4.2-Linux-x86_64/bin/innobackupex –defaults-file=/etc/my.cnf –stream=tar /tmp –user system –password 'Mysql#2015' | gzip ->
/backup/xtra/xtra_fullbackup_20160501.tar.gz
–拷贝备份到节点2
[root@localhost backup]# scp /backup/xtra/xtra_fullbackup_20160501.tar.gz root@192.168.78.137:/backup/20160501
解压备份到数据文件目录
[root@localhost 5505]# tar xivfz xtra_fullbackup_20160501.tar.gz -C /mysql_data/5505
在节点2上面需要安装Xtraback
–使用Xtrabackup准备数据、应用日志,使数据文件达到一致性的状态
[root@localhost bin]# ./innobackupex –defaults-file=/mysql_data/cnf/my.cnf –apply-log /mysql_data/5505
…..
InnoDB: Doing recovery: scanned up to log sequence number 68405269 (0%)
InnoDB: Doing recovery: scanned up to log sequence number 68405269 (0%)
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 414, file name /log/binlog/mysql-bin.000012
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait …
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: 5.7.11 started; log sequence number 68405269
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 68405621
160430 23:51:25 completed OK!
–在节点1数据库上面创建复制专用账户
mysql> grant replication slave on *.* to 'repl'@'192.168.78.%' identified by 'Mysql#2015';
Query OK, 0 rows affected (0.04 sec)
–配置节点2的配置文件
[root@localhost 5505]# vim /mysql_data/cnf/my.cnf
# Log
server-id = 200
log-bin = /mysql_log/binlog/mysql-bin
relay-log = /mysql_log/binlog/product-relay-bin
relay-log-index = /mysql_log/binlog/product-relay-index
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = MIXED
–启动节点2的Mysql服务
[root@localhost bin]# /data/bin/mysqld_safe –defaults-file=/mysql_data/cnf/my.cnf &
–配置Slave节点复制环境
查询Slave节点连接Master节点的二进制文件和位置
使用Xtrabackup备份时,在xtrabackup_binlog_info文件中会保存这部分信息
[root@localhost 5505]# more xtrabackup_binlog_info
mysql-bin.000012 414
–在节点2执行CHANGE MASTER语句
mysql> change master to
-> master_host='192.168.78.141',
-> master_port=3306,
-> master_user='repl',
-> master_password='Mysql#2015',
-> master_log_file='mysql-bin.000012',
-> master_log_pos=414;
Query OK, 0 rows affected (0.13 sec)
–启动应用线程
mysql> start slave;
–查看同步状态
mysql> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.78.141
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000013
Read_Master_Log_Pos: 341
Relay_Log_File: product-relay-bin.000003
Relay_Log_Pos: 487
Relay_Master_Log_File: mysql-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
–搭建节点2到节点1的复制
–在节店1的配置文件中,增加中继日志的设置
[root@localhost log]# vim /etc/my.cnf
relay-log = /log/binlog/product-relay-bin
relay-log-index = /log/binlog/product-relay-index
–重启节点1的数据库
[root@localhost tmp]# /software/bin/mysqladmin -usystem -p'system' shutdown
160512 02:47:54 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
[1]+ Done /software/bin/mysqld_safe –defaults-file=/etc/my.cnf (wd: ~)
(wd now: /tmp)
[root@localhost tmp]# /software/bin/mysqld_safe –defaults-file=/etc/my.cnf &
[1] 40246
[root@localhost tmp]# 160512 02:48:13 mysqld_safe Logging to '/log/err.log'.
160512 02:48:13 mysqld_safe Starting mysqld daemon with databases from /data
mysql> show variables like 'relay_log%';
+———————–+———————————+
| Variable_name | Value |
+———————–+———————————+
| relay_log | /log/binlog/product-relay-bin |
| relay_log_index | /log/binlog/product-relay-index |
| relay_log_info_file | relay-log.info |
| relay_log_purge | ON |
| relay_log_recovery | OFF |
| relay_log_space_limit | 0 |
+———————–+———————————+
6 rows in set (0.00 sec)
–在节点1上面增加全局只读锁,如果应用只连接到一个节点,如节点1,这一步可以忽略
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
–查看节点2当前的日志名称和位置,用于下面在节点1的change master to命令
mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000006 | 14078491 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
–在节点1执行CHANGE MASTER语句
mysql> change master to
-> master_host='192.168.78.137',
-> master_port=5505,
-> master_user='repl',
-> master_password='Mysql#2015',
-> master_log_file='mysql-bin.000006',
-> master_log_pos=14078491;
Query OK, 0 rows affected (0.13 sec)
–启动应用线程
mysql> start slave;
–节点1释放全局只读锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
–查看同步状态
mysql> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.78.137
Master_User: repl
Master_Port: 5505
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 14078491
Relay_Log_File: product-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在两个节点的配置文件中增加序列的相关参数,以避免生成的序列相同而产生冲突
–节点1
节点1上序列从1开始,增长值为2,即为奇数,如1、3、5
## set this to server-id value
auto_increment_offset = 1
## set this to the number of mysql servers
auto_increment_increment = 2
–节点2
节点2上序列从2开始,增长值为2,即为奇数,如2、4、6
## set this to server-id value
auto_increment_offset = 2
## set this to the number of mysql servers
auto_increment_increment = 2
看完上述内容,你们掌握MySQL 5.5 主主复制搭建过程是怎样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/201834.html