MySQL双主同步

Posted song-wentao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL双主同步相关的知识,希望对你有一定的参考价值。

只粘实现的主要步骤,详细步骤见百度

主库A 172.21.25.71

创建一个用于slave和master通信的用户账号
        打开mysql会话shell>mysql -hlocalhost -uname -ppassword
        创建用户并授权:用户:slave密码:slave123456
        mysql> CREATE USER slave@% IDENTIFIED BY slave123456;#创建用户
        mysql> GRANT REPLICATION SLAVE ON *.* TO slave@%;#分配权限
        mysql>flush privileges;   #刷新权限
获得master二进制日志文件名及位置
		查看master状态,记录二进制文件名(mysql-bin.000003)和位置(73):
mysql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000041 |      741 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

使用master分配的用户账号读取master二进制日志
        mysql> CHANGE MASTER TO
        MASTER_HOST=172.21.25.172,
        MASTER_USER=slave,
        MASTER_PASSWORD=slave123456,
        MASTER_LOG_FILE=mysql-bin.000001,
        MASTER_LOG_POS=979;
    启用slave服务
        mysql>start slave;
    查看状态
        mysql> show slave status\G;

    主库重启

主库B 172.21.25.72

操作步骤通上

mysql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000019 |      741 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

使用master分配的用户账号读取master二进制日志
        mysql> CHANGE MASTER TO
        MASTER_HOST=172.21.25.171,
        MASTER_USER=slave,
        MASTER_PASSWORD=slave123456,
        MASTER_LOG_FILE=mysql-bin.000001,
        MASTER_LOG_POS=979;
    启用slave服务
        mysql>start slave;
    查看状态
        mysql> show slave status\G;

其他配置解释

replicate-do-db 设定需要复制的数据库(多数据库使用逗号,隔开)

replicate-ignore-db 设定需要忽略的复制数据库 (多数据库使用逗号,隔开)

replicate-do-table 设定需要复制的表

replicate-ignore-table 设定需要忽略的复制表

replicate-wild-do-table 同replication-do-table功能一样,但是可以通配符

replicate-wild-ignore-table 同replication-ignore-table功能一样,但是可以加通配符
replicate-wild-do-table=db_name.% 只复制哪个库的哪个表

replicate-wild-ignore-table=mysql.% 忽略哪个库的哪个表


问题处理整理 ====================================
#跳过一个事务
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1        

 

附录

配置文件

my-71.cnf

[client]
socket=/home/mysql/mysql.sock
port=3306

[mysqld]
user=mysql
port=3306
server-id=1
max_connections=2000
datadir=/home/mysql/data
#basedir=/home/mysql
socket=/home/mysql/mysql.sock
pid_file=/home/mysql/mysql.pid
default-storage-engine=INNODB
character_set_server=utf8mb4

# 双主
log-bin=binlog 
log-bin-index=binlog.index
relay_log=relay-log
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
replicate-ignore-db=otter
replicate-ignore-db=hh_position

replicate-do-db=data_center
replicate-do-db=hh_data_center
replicate-do-db=test


# INNODB
innodb_buffer_pool_size=60G
innodb_log_file_size=1G
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_open_files=1000

# MyISAM
key_buffer_size=8M

# logging
log_error ="error.log"
log-output=FILE
general-log=1
general_log_file="mysql.log"
slow-query-log=1
slow_query_log_file="mysql-slow.log"
long_query_time=3

# other
tmp_table_size=32M
open_files_limit=65535

my-72.cnf

[client]
socket=/home/mysql/mysql.sock
port=3306

[mysqld]
user=mysql
port=3306
server-id=2
max_connections=2000
datadir=/home/mysql/data
#basedir=/home/mysql
socket=/home/mysql/mysql.sock
pid_file=/home/mysql/mysql.pid
default-storage-engine=INNODB
character_set_server=utf8mb4

# 双主
log-bin=binlog 
log-bin-index=binlog.index
relay_log=relay-log
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
replicate-ignore-db=otter
replicate-ignore-db=hh_position

replicate-do-db=data_center
replicate-do-db=hh_data_center
replicate-do-db=test

# INNODB
innodb_buffer_pool_size=20G
innodb_log_file_size=1G
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_open_files=1000

# MyISAM
key_buffer_size=8M

# logging
log_error ="error.log"
log-output=FILE
general-log=1
general_log_file="mysql.log"
slow-query-log=1
slow_query_log_file="mysql-slow.log"
long_query_time=3

# other
tmp_table_size=32M
open_files_limit=65535

 

以上是关于MySQL双主同步的主要内容,如果未能解决你的问题,请参考以下文章

MySQL双主同步

MySQL主主(双主)数据同步

MySQL主主(双主)数据同步

MySQL热机双备之双主同步复制配置

我C,MySQL双主架构,原来能这么玩

Mysql DBA 高级运维学习笔记-mysql双主及多主同步过程