mysql5.7基于事务的主从配置

mysql主从配置

10.220.87.144 33308 master
10.220.87.145 33308 slave

单节点的mysql正常安装,在两台上安装完后,修改配置。

mysql单点安装

百度搜离线安装。

master

1.my.cnf默认单节点的配置

[client]
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
default-character-set=utf8

[mysqld]
interactive_timeout=1800
wait_timeout=600
max_connections=2000
basedir = /data/ailog4x/apps/mysql
datadir = /data/ailog4x/apps/mysql/data
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slow_query_log=on

[mysqld_safe]
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
pid-file = /data/ailog4x/apps/mysql/data/mysqld.pid

2.my.cnf设置主配置

[client]
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
default-character-set=utf8

[mysqld]
interactive_timeout=1800
wait_timeout=600
max_connections=2000
basedir = /data/ailog4x/apps/mysql
datadir = /data/ailog4x/apps/mysql/data
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slow_query_log=on

innodb-buffer-pool-size=50G
thread_cache_size=32
tmp_table_size=512M
max_heap_table_size=512M
character_set_server=utf8
init_connect='SET NAMES UTF8'
max_allowed_packet=50M
language=/data/ailog4x/apps/mysql/share/english

server_id=44
log-bin=mysql-bin
binlog_format=row
#binlog-do-db=log4xdb
#binlog-ignore-db=information_schema
#binlog-ignore-db=mysql
#binlog-ignore-db=performance_schema
#binlog-ignore-db=test
log-slave-updates

gtid_mode = ON
enforce_gtid_consistency = ON

[mysqld_safe]
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
pid-file = /data/ailog4x/apps/mysql/data/mysqld.pid

3.配置说明

#以下的配置,为了提高查询做的配置,通用
innodb-buffer-pool-size=50G
thread_cache_size=32
tmp_table_size=512M
max_heap_table_size=512M
character_set_server=utf8
init_connect='SET NAMES UTF8'
max_allowed_packet=50M
language=/data/ailog4x/apps/mysql/share/english

#每个节点的server_id一定不相同
server_id=44
log-bin=mysql-bin
#binlog_format值一般用row,专业dba告知,就不要用其他的值了。
binlog_format=row
#同步哪个库,不同步哪个库,就不要设置了,
#binlog-do-db=log4xdb
#binlog-ignore-db=information_schema
#binlog-ignore-db=mysql
#binlog-ignore-db=performance_schema
#binlog-ignore-db=test
log-slave-updates
#通过事物的方式去同步数据,而不是通过设置log_file去同步。
gtid_mode = ON
enforce_gtid_consistency = ON

4.master上执行

grant replication slave,super,reload on *.* to [email protected] identified by 'log4x123bak';
flush privileges;
#创建用户同步数据的用户log4xbak,密码为log4x123bak

5.show master status\G;

 show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 232565
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: a1aed7b3-1bae-11ee-a270-34b35427aee1:1-658
1 row in set (0.00 sec)

ERROR: 
No query specified

slave

1.my.cnf默认的单节点配置

[client]
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
default-character-set=utf8

[mysqld]
interactive_timeout=1800
wait_timeout=600
max_connections=2000
basedir = /data/ailog4x/apps/mysql
datadir = /data/ailog4x/apps/mysql/data
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slow_query_log=on

[mysqld_safe]
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
pid-file = /data/ailog4x/apps/mysql/data/mysqld.pid

2.my.cnf设置从配置

# cat my.cnf
[client]
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
default-character-set=utf8

[mysqld]
interactive_timeout=1800
wait_timeout=600
max_connections=2000
basedir = /data/ailog4x/apps/mysql
datadir = /data/ailog4x/apps/mysql/data
port = 33308
socket = /data/ailog4x/apps/mysql/mysql.sock
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slow_query_log=on

innodb-buffer-pool-size=50G
thread_cache_size=32
tmp_table_size=512M
max_heap_table_size=512M
character_set_server=utf8
init_connect='SET NAMES UTF8'
max_allowed_packet=50M
language=/data/ailog4x/apps/mysql/share/english

server_id=45
log-bin=mysql-bin
#binlog-do-db=log4xdb
#binlog-ignore-db=information_schema
#binlog-ignore-db=mysql
#binlog-ignore-db=performance_schema
#binlog-ignore-db=test
log-slave-updates
binlog_format=row
gtid_mode = ON
enforce_gtid_consistency = ON
[mysqld_safe]
log-error= /data/ailog4x/apps/mysql/logs/mysqld.log
pid-file = /data/ailog4x/apps/mysql/data/mysqld.pid

3.配配说明见 master中3节配置说明

4.change to master

从节点启动后,执行

change master to master_host='10.220.87.144',master_user='log4xbak',master_password='log4x123bak',master_port='33308';

#这里要指定端口,否则默认是3306,当数据库不是默认端口时,就一直错误,同步不了。
start slave; 启动从
stop  slave; 停止从
reset slave; 如果启动有问题,重置从节点

4.show slave status\G;

# show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.87.144
                  Master_User: log4xbak
                  Master_Port: 33308
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 232565
               Relay_Log_File: log4x7-relay-bin.000003
                Relay_Log_Pos: 676
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 232565
              Relay_Log_Space: 233286
              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: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 44
                  Master_UUID: a1aed7b3-1bae-11ee-a270-34b35427aee1
             Master_Info_File: /data/ailog4x/apps/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: a1aed7b3-1bae-11ee-a270-34b35427aee1:1-658
            Executed_Gtid_Set: 217f92a5-1bdc-11ee-b702-525400cad9b5:1-673,
a1aed7b3-1bae-11ee-a270-34b35427aee1:1-658
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

判断说明

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            。。。
                    SQL_Delay: 0
从slave的配置中,能看到主的一些信息。ip,端口。以及同步的用户名。

遇到问题

1.Slave_SQL_Running: no

             Slave_IO_Running: Yes
            Slave_SQL_Running: no

这是因为从库有自己操作过一些内容,导致主,从库中的数据不一致。

将主库导出一份到从库中,即可恢复成yes

主:跟着提示,一步步执行下去就行,最后的warning可以忽略了。

bin/mysqldump -ulog4x -plog4x@123 --all-databases > 144.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect


[ailog4x@log4x6 mysql]$ 
[ailog4x@log4x6 mysql]$ bin/mysqldump -ulog4x -plog4x@123 -S /data/ailog4x/apps/mysql/mysql.sock --all-databases > 144.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
mysqldump: Error: 'Access denied; you need (at least one of) the PROCESS privilege(s) for this operation' when trying to dump tablespaces


[ailog4x@log4x6 mysql]$ 
[ailog4x@log4x6 mysql]$ bin/mysqldump -uroot -plog4x@123 -S /data/ailog4x/apps/mysql/mysql.sock --all-databases > 144.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 


[ailog4x@log4x6 mysql]$ 
[ailog4x@log4x6 mysql]$ bin/mysqldump -uroot -plog4x@123 -S /data/ailog4x/apps/mysql/mysql.sock --set-gtid-purged=OFF  --all-databases > 144.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

从:导入数据

bin/mysql -uroot -plog4x@123 <144.sql 
bin/mysql -uroot -plog4x@123 -S /data/ailog4x/apps/mysql/mysql.sock  <144.sql

当数据一致后,在修改主中的数据,从中的数据也随之更新。

你可能感兴趣的:(数据库,Linux,系统架构,数据库)