mysql不停机主从搭建

  1. 主库master的my.cnf配置
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
innodb_buffer_pool_size = 2048M
innodb_thread_concurrency=8
basedir =/usr/local/mysql
datadir=/app/mysql/data
socket= /usr/local/mysql/tmp/mysql.sock
tmpdir = /tmp
user = mysql

max_allowed_packet = 100M
skip-name-resolve
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
port=3306
collation-server = utf8mb4_general_ci
lower_case_table_names=1
character-set-server = utf8mb4
max_connections=3600
# log-bin=mysql-bin
server-id=11
#### gtid ####
gtid_mode = on
# 强制gtid一致性
enforce_gtid_consistency = 1
binlog_gtid_simple_recovery = 1
#### log ####
log_timestamps=system
log_bin_index = /app/mysql/log/mysql-bin.index
# 默认为mixed混合模式,更改成row复制,为了数据一致性
binlog_format = row
relay_log_recovery=ON
relay_log=/app/mysql/log/mysql-relay-bin
relay_log_index=/app/mysql/log/mysql-relay-bin.index
log_error = /app/mysql/log/mysql-error.log
# binlog过期时间
expire_logs_days=30
# 从库binlog才会记录主库同步的操作日志
log-slave-updates = 1
# 开启二进制日志
log_bin = /app/mysql/log/mysql-bin
#### replication ####
replicate_wild_ignore_table = information_schema.%,performance_schema.%,sys.%
#### semi sync replication settings #####
plugin_dir=/usr/local/mysql/lib/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000

[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

[mysqld_safe]
log-error=/app/mysql/log/mysqld.log
pid-file=/app/mysql/run/mysqld.pid
  1. 从库slave的my.cnf配置
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
innodb_buffer_pool_size = 2048M
innodb_thread_concurrency=8
basedir =/usr/local/mysql
datadir=/app/mysql/data
socket= /usr/local/mysql/tmp/mysql.sock
tmpdir = /tmp
user = mysql

max_allowed_packet = 100M
skip-name-resolve
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
port=3306
collation-server = utf8mb4_general_ci
lower_case_table_names=1
character-set-server = utf8mb4
max_connections=3600
# log-bin=mysql-bin
server-id=12
# 以防人为误操作从库数据
read_only= 1

gtid_mode = on
enforce_gtid_consistency = 1
binlog_gtid_simple_recovery = 1

log_timestamps=system
log_bin_index = /app/mysql/log/mysql-bin.index

binlog_format = row
relay_log_recovery=ON
relay_log=/app/mysql/log/mysql-relay-bin
relay_log_index=/app/mysql/log/mysql-relay-bin.index
log_error = /app/mysql/log/mysql-error.log

expire_logs_days=30

log-slave-updates = 1
log_bin = /app/mysql/log/mysql-bin

replicate_wild_ignore_table = information_schema.%,performance_schema.%,sys.%

plugin_dir=/usr/local/mysql/lib/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

[mysqld_safe]
log-error=/app/mysql/log/mysqld.log
pid-file=/app/mysql/run/mysqld.pid

cnf相关配置含义:

  • datadir, basedir, tmpdir 分别为数据文件位置、数据库程序安装位置、临时文件位置
  • server_id 实例id,注意,同一集群机器的 server_id 不能相同
  • read_only 是否只读, 一般在备库设置
  • log_bin, log_bin_index 二进制日志位置、二进制日志索引文件位置
  • binlog_format 二进制日志格式,row 表示记录每条数据变化情况、statement 表示记录相关 sql 语句、mixed 表示两种混用,在搭建集群的时候建议使用 row 格式,如果是用 sql 语句来同步数据很容易出现数据不一致的情况
  • relay_log_recovery slave 宕机后,假如中继日志损坏,则重新拉取日志,为了保证中继日志完整性,建议开启
  • relay_log, relay_log_index 中继日志以及中继日志索引文件位置
  • log_error 错误日志位置
  • replicate_wild_ignore_table 同步时需要忽略的表,这里我们忽略了系统统计表,如果出现奇怪的同步失败情况,可以尝试开启
  • plugin_dir 插件位置
  • plugin_load 启动时需要加载的插件
  • loose_rpl_semi_sync_master_enabled 是否开启无损半同步复制-主库(建议主备都开启,方便主备切换)
  • loose_rpl_semi_sync_slave_enabled 是否开启无损半同步复制-备库(建议主备都开启,方便主备切换)
  • 一般具有super权限的root等用户只能在本地登录,不会发生数据变化,其他远程连接的应用用户只按需分配为select,insert,update,delete等权限,保证没有super权限,则只需要将salve设定“read_only=1”模式,即可保证主从同步,又可以实现从库只读。
  1. 配置完,重置一下autoid(如果是用同一个镜像就需要),mv /app/mysql/data/auto.cnf /app/mysql/data/auto.cnf.bak,最后重启一下mysql服务:service mysql restart

出现:Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ ,则需要重新建立一下软链接:ln -s /usr/local/mysql/tmp/mysql.sock /tmp/mysql.sock

  1. 登录主库master,创建从库用来复制的账号信息
mysql -uroot -p

CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'dsdfd..';
GRANT replication slave,replication client ON *.* TO 'slave'@'%';

flush privileges;
  1. 登录从库服务器,测试新账号的连通性
mysql -uslave -pdsdfd.. -h192.168.100.30
  1. 查看当前主库日志文件信息
show master status;

+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     3291 |              |                  | fc28cb61-b371-11ed-9426-000c29a77e07:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
  1. 登录从库slave
# 停止从库
mysql> stop slave;
# 重置主从配置
mysql> reset slave all;
# slave节点登录MySQL与master 进行关联,file和pos取主库日志信息
mysql> change master to master_host='192.168.77.128',master_port=3306,master_user='slave',master_password='dsdfd..', master_log_file='mysql-bin.000001',master_log_pos=3291;

# 启动
mysql> start slave;
  1. 查看从库状态,我们可以从 Slave_IO_Running, Slave_SQL_Running, Seconds_Behind_Master 这三个参数可以判断出同步状态是否正常
mysql> show slave status\G;

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.77.128
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 3291
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 2774
        Relay_Master_Log_File: mysql-bin.000001
             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: information_schema.%,performance_schema.%,sys.%
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 3291
              Relay_Log_Space: 2982
              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: 11
                  Master_UUID: fc28cb61-b371-11ed-9426-000c29a77e07
             Master_Info_File: mysql.slave_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: fc28cb61-b371-11ed-9426-000c29a77e07:4-6
            Executed_Gtid_Set: fc28cb61-b371-11ed-9426-000c29a77e07:4-6
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

  1. 同步测试
  2. 对于主库已在使用,在线搭建一个从库,利用dump导入从库,再设置关联同步
# 从库使用mysqldump远程导出数据库
mysqldump -uasi -pAss6.. -h192.125.86.52 --all-databases --master-data=2 --single-transaction --quick --set-gtid-purged=ON --force > /app/mysql/dump/as3d.sql

# 查看导出来的puraged情况
grep GTID_PURGED /app/mysql/dump/as3d.sql

# 从库设置puraged,告诉从库不需要执行这批次事务
SET @@GLOBAL.GTID_PURGED='78336cdc-8cfb-11e6-ba9f-000c29328504:1-38';

# 登录从库执行reset master,重置从库的binlog
mysql> reset master;

# 导入主库数据
mysql -uroot -pcodesnake < /app/mysql/dump/as3d.sql

# 查询从库数据是否正常
mysql> show databases;

# 查看dump出来的log_pos
head -n 100 /app/mysql/dump/as3d.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=4805477;

# 执行change master关联到主库
mysql> change master to master_host='192.125.86.52',master_port=3306,master_user='slave',master_password='dsdfd..', master_log_file='mysql-bin.000001',master_log_pos=4805477;
mysql> start slave;
mysql> show slave status \G;
  1. 主从库灾难切换,详见:https://blog.csdn.net/sinat_36757755/article/details/124049382

你可能感兴趣的:(mysql,数据库)