MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。
一、搭建环境描述:
主机A IP:192.168.1.201
主机B IP:192.168.1.202
操作系统:centos6.5-x86
Mysql版本:mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
安装所需依赖包:yum install libaio-devel –y
关闭SElinux及IPtables。
二、配置主主复制:
1、修改配置文件
vim /etc/my.cnf
[mysqld]########basic settings########server-id = 1 #另一台ID不同port = 3306user = mysqlbind_address = 192.168.1.201 #另一台IP不用autocommit = 0character_set_server=utf8mb4skip_name_resolve = 1max_connections = 800max_connect_errors = 1000datadir = /usr/local/mysql/datatransaction_isolation = READ-COMMITTEDexplicit_defaults_for_timestamp = 1join_buffer_size = 134217728tmp_table_size = 67108864tmpdir = /tmpmax_allowed_packet = 16777216sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"interactive_timeout = 1800wait_timeout = 1800read_buffer_size = 16777216read_rnd_buffer_size = 33554432sort_buffer_size = 33554432########log settings########log_error = error.logslow_query_log = 1slow_query_log_file = slow.loglog_queries_not_using_indexes = 1log_slow_admin_statements = 1log_slow_slave_statements = 1log_throttle_queries_not_using_indexes = 10expire_logs_days = 90long_query_time = 2min_examined_row_limit = 100########replication settings########master_info_repository = TABLErelay_log_info_repository = TABLElog_bin = bin.logsync_binlog = 1gtid_mode = onenforce_gtid_consistency = 1log_slave_updatesbinlog_format = row relay_log = relay.logrelay_log_recovery = 1binlog_gtid_simple_recovery = 1slave_skip_errors = all# slaveslave-parallel-type = LOGICAL_CLOCKslave-parallel-workers = 0#slave-parallel-workers = 16########innodb settings########innodb_buffer_pool_size = 750M //内存的75%比较合适innodb_buffer_pool_instances = 16innodb_buffer_pool_load_at_startup = 1innodb_buffer_pool_dump_at_shutdown = 1innodb_lru_scan_depth = 2000innodb_lock_wait_timeout = 10000innodb_io_capacity = 4000innodb_io_capacity_max = 8000innodb_flush_method = O_DIRECTinnodb_flush_neighbors = 1innodb_log_file_size = 4Ginnodb_log_buffer_size = 16777216innodb_purge_threads = 4innodb_large_prefix = 1innodb_thread_concurrency = 64innodb_print_all_deadlocks = 1innodb_strict_mode = 1innodb_sort_buffer_size = 67108864 innodb_flush_log_at_trx_commit = 2innodb_read_io_threads = 16innodb_write_io_threads = 16 ########semi sync replication settings########plugin_dir=/usr/local/mysql/lib/pluginplugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"loose_rpl_semi_sync_master_enabled = 1loose_rpl_semi_sync_slave_enabled = 1loose_rpl_semi_sync_master_timeout = 5000[mysqld-5.7]innodb_buffer_pool_dump_pct = 40innodb_page_cleaners = 4innodb_undo_log_truncate = 1innodb_max_undo_log_size = 2Ginnodb_purge_rseg_truncate_frequency = 128binlog_gtid_simple_recovery=1log_timestamps=systemtransaction_write_set_extraction=MURMUR32show_compatibility_56=on |
2、初始化mysql数据库
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize /etc/init.d/mysqld start
3、设置密码
mysql5.7不允许密码为空了,所以必须得设置密码
grep "temporary password" /usr/local/mysql/data/error.log
2016-07-04T09:50:17.455227+08:00 1 [Note] A temporary password is generated for root@localhost: wLEM;g+?&1eF/usr/local/mysql/bin/mysql -uroot -p #输入日志里的密码mysql> alter user ‘root‘@‘localhost‘ identified by ‘123456‘;mysql> flush privileges;4、配置数据同步
#两台Mysql同时添加同步用户
1 2 3 | mysql> create user ‘repl‘@‘192.168.1.%‘ identified by ‘123456‘;mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘192.168.1.%‘;mysql> flush privileges; |
锁一下表,追平两台机器数据,然后解锁。
先看看GTID是否打开
1 2 3 4 5 6 7 8 9 10 11 | mysql> show global variables like ‘%gtid%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| enforce_gtid_consistency | ON || gtid_executed | || gtid_mode | ON || gtid_owned | || gtid_purged | |+--------------------------+-------+5 rows in set (0.10 sec) |
#说明gtid功能已启动。
GTID同步数据不用再记录对方的log文件和位置了,用master_auto_position=1就行,不过你用老的方法查看master的logfile和logpos,同步也是可以的。
#在两台Mysql上都运行,IP得互指
mysql> change master to master_host=‘192.168.1.201‘, master_user=‘repl‘,master_password=‘123456‘,master_auto_position=1;
mysql> start slave;
show slave status\G ;
#表示同步的文件和位置
Master_Log_File:
Read_Master_Log_Pos: 4
#显示下面表示工作正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#表示当前同步的数据库
Replicate_Do_DB: nzabbix,szabbix
过程中可能遇到的错误及解决方法:
错误一:
mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
解决方法:
mysql> reset slave;
错误二:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
解决方法:
修改my.cnf配置文件中,slave_skip_errors = all
本文出自 “小飞侠” 博客,请务必保留此出处http://jack88.blog.51cto.com/12348665/1901252
原文地址:http://jack88.blog.51cto.com/12348665/1901252