MySQL主从扩展

Posted

tags:

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

常见问题排查

1.数据不一致:

SHOW SLAVE STATUS \G;

Last_Errno: 1062

Last_Error: Error ‘Duplicate entry ‘xxxn-66-77‘ for key1‘ on query. Default database: ‘guild‘. Query: ‘insert into pynpcrecord setMapCode = ‘xxxn‘, UpdateTime = ‘2015-08-07 00:00:32‘‘

解决方法:在从库上将重复的主键记录删除,再次重启主从

deletefrom xxxx where 主键=yyyy;

  stopslave;start slave;

解决方法:停掉主从同步,忽略一次错误,再开启同步:

stop slave;

set global sql_slave_skip_counter=1;startslave;

主主架构方案

1.思路:

1.两台mysql都可读写,互为主备,默认只使用一台(masterA)负责数据的写入,另一台(masterB)备用;

2.masterA是masterB的主库,masterB又是masterA的主库,它们互为主从;

3.两台主库之间做高可用,可以采用keepalived等方案(使用VIP对外提供服务);

4.所有提供服务的从服务器与masterB进行主从同步(双主多从);

5.建议采用高可用策略的时候,masterA或masterB均不因宕机恢复后而抢占VIP(非抢占模式);

2.操作:修改my.cnf

修改A的

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

[mysqld]
basedir = /usr/local/mysql
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
log-error = /usr/local/mysql/data/mysql.err

server-id = 1
auto_increment_offset = 1
auto_increment_increment = 2                                            #奇数ID

log-bin = mysql-bin                                                     #打开二进制功能,MASTER主服务器必须打开此项
binlog-format=ROW
binlog-row-p_w_picpath=minimal
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=0
sync_binlog=0
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
#expire_logs_days=5
max_binlog_size=1024M                                                   #binlog单文件最大值

replicate-ignore-db = mysql                                             #忽略不同步主从的数据库
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = test
replicate-ignore-db = zabbix

max_connections = 3000
max_connect_errors = 30

skip-character-set-client-handshake                                     #忽略应用程序想要设置的其他字符集
init-connect=‘SET NAMES utf8‘                                           #连接时执行的SQL
character-set-server=utf8                                               #服务端默认字符集
wait_timeout=1800                                                       #请求的最大连接时间
interactive_timeout=1800                                                #和上一参数同时修改才会生效
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES                     #sql模式
max_allowed_packet = 10M
bulk_insert_buffer_size = 8M
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M
key_buffer_size = 256M
read_buffer_size = 16K

skip-name-resolve
slow_query_log=1
long_query_time = 6
slow_query_log_file=slow-query.log
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]

修改B的

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

[mysqld]
basedir = /usr/local/mysql
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
log-error = /usr/local/mysql/data/mysql.err

server-id = 2
auto_increment_offset = 2
auto_increment_increment = 2                                            #偶数ID

log-bin = mysql-bin                                                     #打开二进制功能,MASTER主服务器必须打开此项
binlog-format=ROW
binlog-row-p_w_picpath=minimal
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=0
sync_binlog=0
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
#expire_logs_days=5
max_binlog_size=1024M                                                   #binlog单文件最大值

replicate-ignore-db = mysql                                             #忽略不同步主从的数据库
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = test
replicate-ignore-db = zabbix

max_connections = 3000
max_connect_errors = 30

skip-character-set-client-handshake                                     #忽略应用程序想要设置的其他字符集
init-connect=‘SET NAMES utf8‘                                           #连接时执行的SQL
character-set-server=utf8                                               #服务端默认字符集
wait_timeout=1800                                                       #请求的最大连接时间
interactive_timeout=1800                                                #和上一参数同时修改才会生效
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES                     #sql模式
max_allowed_packet = 10M
bulk_insert_buffer_size = 8M
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M
key_buffer_size = 256M
read_buffer_size = 16K

skip-name-resolve
slow_query_log=1
long_query_time = 6
slow_query_log_file=slow-query.log
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]

3.初始化:

cd /usr/local/mysql
scripts/mysql_install_db --user=mysql

4.A 上面:

mysql> grant replication slave on *.* to ‘repl‘@‘192.168.10.12‘ identified by ‘123456‘;
mysql> flush privileges;

B上面:

mysql> grant replication slave on *.* to ‘repl‘@‘192.168.10.11‘ identified by ‘123456‘;
mysql> flush privileges;

5.配置同步信息:

A上面

mysql> change master to master_host=‘192.168.10.12‘,master_port=3306,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=437;

mysql> start slave;

mysql> show slave status\G;

B上面

mysql> change master to master_host=‘192.168.10.11‘,master_port=3306,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=120;

start slave;

mysql> show slave status\G;

完成

一主多从

主要配置:

1.修改master上的配置文件my.cnf。
在[mysqld]下添加如下字段:

server-id = 1
log-bin=mysql-bin
binlog-do-db=YYY //需要同步的数据库
binlog-ignore-db=mysql //被忽略的数据库
binlog-ignore-db=information-schema //被忽略的数据库

在master上为slave添加一个同步账号

mysql> grant replication slave on *.* to ‘affairlog‘@‘192.168.2.182‘ identified by ‘pwd123‘;
//在slave1上登陆成功
mysql> grant replication slave on *.* to ‘affairlog‘@‘192.168.2.111‘ identified by ‘pwd123‘;
//在slave2上登陆成功

保存后,重启master的mysql服务:

service mysql restart;

用show master status命令查看日志情况

mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000087
Position: 106
Binlog_Do_DB: YYY
Binlog_Ignore_DB: mysql,information-schema
1 row in set (0.00 sec)
2.修改slave1上的配置文件my.cnf。
在[mysqld]下添加如下字段
[[email protected] ~]# vi /etc/my.cnf
server-id=182
master-host=192.168.3.101
master-user= affairlog
master-password=pwd123
master-port=3306
master-connect-retry=60
replicate-do-db=YYY //同步的数据库
replicate-ignore-db=mysql //被忽略的数据库
replicate-ignore-db=information-schema //被忽略的数据库

保存后,重启slave的mysql服务:

service mysql restart;

修改slave2上的配置文件my.cnf,和上面类似,只是把server-id改下,为了方便,我都用了相应的ip某位,
so,slave2上我设置的server-id是111。
在进入两个slave机中的mysql。

mysql>start slave;
mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.101
Master_User: affairlog
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000087
Read_Master_Log_Pos: 106
Relay_Log_File: vm111-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000087
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: YYY
Replicate_Ignore_DB: mysql,information-schema
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: 106
Relay_Log_Space: 406
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 in set (0.00 sec)

如果两个slave中的Slave_IO_Running、Slave_SQL_Running状态均为Yes则表明设置成功。

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

Mysql主从扩展之问题及解决方法

MySQL主从扩展

MySQL的主从复制

mysql的主从复制

Mysql-主从复制架构的扩展主主-多从

mysql扩展:xtrabackup备份和做主从