mysql互为主从

业务场景:

目前项目由于业务需要,增加一台备机,在主机宕机时(拔掉网线等),备机工作.主备机使用不同的数据库.

配置描述:

        - 主机: 192.168.101.179,端口:3306,版本:5.7.35

        - 备机: 192.168.101.57,端口:3306,版本:5.7.35

配置注意:

注意: 配置时一定要停止写入数据 不然主从库数据不一致会出现无法同步的情况

配置:

1.打开主数据库的my.cnf文件 编辑如下

server-id = 1 #主库id
skip-name-resolve #忽略客户端链接时的主机名日志打印(可提高速度,非必选)
long_query_time=1 #查询速度低于1秒的进行日志记录
innodb-file-per-table=1 #开启独立表空间
innodb_flush_log_at_trx_commit = 2 #提交事务的时候将 redo 日志写入磁盘中
log_warnings = 1 #记录警告标识
connect_timeout = 60 #链接超时时间
net_read_timeout = 120 #数据读取超时时间
performance_schema_max_table_instances = 400 #检测表对象的最大数量
log-bin=master-bin # bin日志名称
sync-binlog=1 #使执行1次写入后,与硬盘同步
binlog-do-db=location #主数据库名称(这里替换成你的数据库名字)
binlog-ignore-db = mysql #忽略以下库的同步
binlog-ignore-db = performance_schema #忽略以下库的同步
binlog-ignore-db = information_schema #忽略以下库的同步
relay-log = relay-log #副本日志名称
relay-log-index = relay-log.index#副本日志索引位置
binlog_format = ROW #复制方法,逐条复制
  1. 打开从数据库的my.cnf文件 编辑如下(除了server-id其他配置都一样)
server-id = 2 #从库id
skip-name-resolve #忽略客户端链接时的主机名日志打印(可提高速度,非必选)
long_query_time=1 #查询速度低于1秒的进行日志记录
innodb-file-per-table=1 #开启独立表空间
innodb_flush_log_at_trx_commit = 2 #提交事务的时候将 redo 日志写入磁盘中
log_warnings = 1 #记录警告标识
connect_timeout = 60 #链接超时时间
net_read_timeout = 120 #数据读取超时时间
performance_schema_max_table_instances = 400 #检测表对象的最大数量
log-bin=master-bin # bin日志名称
sync-binlog=1 #使执行1次写入后,与硬盘同步
binlog-do-db=location #主数据库名称(这里替换成你的数据库名字)
binlog-ignore-db = mysql #忽略以下库的同步
binlog-ignore-db = performance_schema #忽略以下库的同步
binlog-ignore-db = information_schema #忽略以下库的同步
relay-log = relay-log #副本日志名称
relay-log-index = relay-log.index#副本日志索引位置
binlog_format = ROW #复制方法,逐条复制

3.登录主库(使用navicat或者直接登录主库)

[root@localhost ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13222
Server version: 5.7.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4.切换数据库

mysql> use location;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

5.查看主库状态

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-bin.000007
         Position: 894227729
     Binlog_Do_DB: location
 Binlog_Ignore_DB: mysql,performance_schema,information_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

记录这些值:

File: master-bin.000007 #当前日志文件的名字
Position: 894227729 #日志文件的位置

6.登录从库,变更从库的复制位置

CHANGE MASTER TO
    MASTER_HOST ='192.168.101.179',
    MASTER_USER ='root',
    MASTER_PASSWORD ='root',
    MASTER_LOG_FILE = 'master-bin.000007',
    MASTER_LOG_POS = 894227729;

7.开启从库复制

start slave;

8.查看从库slave状态

show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.179
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000007
          Read_Master_Log_Pos: 894227729
               Relay_Log_File: relay-log.000037
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-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: 4701104
              Relay_Log_Space: 689
              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: db36dfe3-9479-11ec-b4a7-7478271b8174
             Master_Info_File: /var/lib/mysql/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)

如果显示如下,则表示主从复制成功:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果其中有一个IO为no 则表示失败,这时可以查看日志,看看失败原因并纠正:

Last_IO_Error: io失败原因
Last_SQL_Error:sql失败原因
Slave_SQL_Running_State:从库sql运行状态

9.查看从库master状态

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-bin.000002
         Position: 4701104 | 
     Binlog_Do_DB: location
 Binlog_Ignore_DB: mysql,performance_schema,information_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

记录一下值

File: master-bin.000002
Position: 4701104

10.登录主库,切换主库master

CHANGE MASTER TO
    MASTER_HOST ='192.168.101.57',
    MASTER_USER ='root',
    MASTER_PASSWORD ='root',
    MASTER_LOG_FILE = 'master-bin.000002',
    MASTER_LOG_POS = 4701104;

11.启动主库slave

start slave;

12.查看主库slave状态

show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.57
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 4701104
               Relay_Log_File: relay-log.000037
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-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: 4701104
              Relay_Log_Space: 689
              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: db36dfe3-9479-11ec-b4a7-7478271b8174
             Master_Info_File: /var/lib/mysql/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)

如果显示如下,则表示主从复制成功:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果其中有一个IO为no 则表示失败,这时可以查看日志,看看失败原因并纠正:

Last_IO_Error: io失败原因
Last_SQL_Error:sql失败原因
Slave_SQL_Running_State:从库sql运行状态

至此配置结束!这时当主库/从库 拔掉网线,在启动时会自动同步从/主库信息

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容