Mysql主主复制配置

1.安装mysql
参考 centos7非root下安装mysql5.7.29
主机A:192.168.62.37
主机B:192.168.62.15

2.主机A 配置my.cnf,内容如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
character-set-server = utf8
port = 3306

basedir=/usrdata/mysql/mysql-5.7.29
datadir=/usrdata/mysql/mysql-5.7.29/data
socket=/usrdata/mysql/mysql-5.7.29/mysql.sock

log_error=/usrdata/mysql/mysql-5.7.29/error.log
pid-file=/usrdata/mysql/mysql-5.7.29/mysql.pid

# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links = 0

#标识唯一id(必须)
server-id=1
#开启二进制日志
log-bin=mysql-bin
#不同步的数据库,可设置多个
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=sys
#指定需要同步的数据库(和slave是相互匹配的),可以设置多个
#binlog-do-db=test

#主-主形式需要多添加的部分
log-slave-updates
#sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 2
replicate-ignore-db = mysql,information_schema,performance_schema,sys

#设置存储模式不设置默认
binlog_format=MIXED
#日志清理时间
expire_logs_days=7
#日志大小
max_binlog_size=100m
#缓存大小
binlog_cache_size=4m
#最大缓存大小
max_binlog_cache_size=521m

[client]
port=3306
default-character-set=utf8
socket=/usrdata/mysql/mysql-5.7.29/mysql.sock

3.主机B 配置my.cnf,内容如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
character-set-server = utf8
port = 3306

basedir=/usrdata/mysql/mysql-5.7.29
datadir=/usrdata/mysql/mysql-5.7.29/data
socket=/usrdata/mysql/mysql-5.7.29/mysql.sock

log_error=/usrdata/mysql/mysql-5.7.29/error.log
pid-file=/usrdata/mysql/mysql-5.7.29/mysql.pid

# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links = 0

#标识唯一id(必须)
server-id=2
#开启二进制日志
log-bin=mysql-bin
#不同步的数据库,可设置多个
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=sys
#指定需要同步的数据库(和slave是相互匹配的),可以设置多个
#binlog-do-db=test

#主-主形式需要多添加的部分
log-slave-updates
#sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2
replicate-ignore-db = mysql,information_schema,performance_schema,sys

#设置存储模式不设置默认
binlog_format=MIXED
#日志清理时间
expire_logs_days=7
#日志大小
max_binlog_size=100m
#缓存大小
binlog_cache_size=4m
#最大缓存大小
max_binlog_cache_size=521m

[client]
port=3306
default-character-set=utf8
socket=/usrdata/mysql/mysql-5.7.29/mysql.sock

4.重启mysql,分别创建用于同步的用户账号
1).配置MasterA主=》MasterB从

# MasterA 主机配置
mysql> grant replication slave on *.* to 'repl'@'192.168.62.15' identified by '123456';
mysql> flush privileges;  #刷新权限

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000007 |      613 |              | information_schema,performance_schema,mysql,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
#File、Position 在配置从机时需要用到

#MasterB 从机配置
mysql> change master to master_host='192.168.62.37',
                 master_user='repl',
                 master_password='123456',
                 master_log_file='mysql-bin.000007',
                 master_log_pos=613;
#启动slave同步进程:
mysql> start slave;
#查看slave状态:
mysql> show slave status\G;

2).配置MasterB主=》MasterA从

# MasterB主机配置
mysql> grant replication slave on *.* to 'repl'@'192.168.62.37' identified by '123456';
mysql> flush privileges;  #刷新权限

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000004 |      613 |              | information_schema,performance_schema,mysql,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
#File、Position 在配置从机时需要用到
#MasterA从机配置
mysql> change master to master_host='192.168.62.15',
                 master_user='repl',
                 master_password='123456',
                 master_log_file='mysql-bin.000004',
                 master_log_pos=613;
#启动slave同步进程:
mysql> start slave;
#查看slave状态:
mysql> show slave status\G;

3).配置成功后,分别查看slave状态

#当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.62.15
                  Master_User: ntrepl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 613
               Relay_Log_File: nt-metra-1-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
           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: 613
              Relay_Log_Space: 532
              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: 2
                  Master_UUID: a0ce91d7-3053-11eb-95b3-fa163e443845
             Master_Info_File: /usrdata/mysql/mysql-5.7.29/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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

5.正确关闭slave及停止mysql服务步骤

关闭MySQL从库
1. 先查看当前的主从同步状态show slave status\G;看是否双yes
2. 分别执行stop slave,停止从库线程
3. 停止从库服务mysqladmin shutdown -u用户名 -p密码
4. 查看是否还有mysql的进程ps -ef | grep mysql
5. 如果部署了多个实例,那每个实例都要按照以上步骤来操作

关闭MySQL主库
1. 停止主库服务mysqladmin shutdown -u用户名 -p密码
2. 查看是否还有mysql的进程ps -ef | grep mysql

6.正确启动mysql主从服务步骤

启动MySQL主库
1. 启动主库服务mysqladmin start -u用户名 -p密码
2. 查看mysql的进程ps -ef | grep mysql

启动MySQL从库
1. 启动从库服务mysqladmin start -u用户名 -p密码
2. 分别启动复制start slave;
3. 检查同步状态show slave status\G;是否双yes
4. 查看mysql的进程ps -ef | grep mysql
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容