为线上运营Mysql数据库设置从库

一、为mysql运营主库添加一个repl 账号

[root@zabbix_server ~]# mysql -uroot -p -S /var/lib/mysql/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 15778982
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> 
mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'xxxxxxxxxx';

二、修改主库my.cnf

增加以下字段

server_id=IP+PORT   标识服务器ID

log-bin=master-221 设置log_bin日志文件名

binlog_format=row 指定日志格式为row

server_id=693306
log-bin=master-18-69
binlog_format=row

三、重启主库mysql

停止mysql实例

[root@server-1 ~]# mysqladmin -uroot -p -S /home/mysql-5.7.26/mysql.sock shutdown

重启mysql实例

[root@server-1 ~]# mysqld_safe --defaults-file=/etc/mysql/my.cnf &

进入mysql

[root@server-1 ~]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql>

查看server_id变量

mysql> show variables like '%servier_id%';
Empty set (0.01 sec)

mysql> show variables like '%server_id%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| server_id      | 693306 |
| server_id_bits | 32     |
+----------------+--------+
2 rows in set (0.01 sec)

mysql> 

已经看到server_id变量已经成功设置。

四、修改从库my.cnf

添加server_id=IP+PORT

server_id=713306

五、重启mysql

[root@localhost mysql]# systemctl restart mysqld
[root@localhost mysql]# 

进入mysql,查看server_id变量

[root@localhost data]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> show variables like '%server_id%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| server_id      | 713306 |
| server_id_bits | 32     |
+----------------+--------+
2 rows in set (0.01 sec)

mysql> 

已经变成我们设置值了,

六、接下来执行从库命令

mysql> change master to
    -> master_host='172.28.18.69',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Zaq1xsw@',
    -> master_log_file='master-18-69.000001',
    -> master_log_pos=419;
Query OK, 0 rows affected, 2 warnings (0.10 sec)

mysql> 

这里的 master_log_file和master_log_pos值,我们可以在主库上运行如下命令得到

mysql> show master statusG;
*************************** 1. row ***************************
             File: master-18-69.000001
         Position: 419
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-7,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-23
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

执行成功后,我们启动从库操作

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> 

查询从库状态

mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.28.18.69
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-18-69.000001
          Read_Master_Log_Pos: 1593
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 1497
        Relay_Master_Log_File: master-18-69.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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1593
              Relay_Log_Space: 1708
              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: 693306
                  Master_UUID: ee3e292b-866b-11e9-9df8-14feb5dc2c77
             Master_Info_File: /home/mysql-5.7.26/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: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-79,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-15
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 
  Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
从库启动成功,同步成功
Exec_Master_Log_Pos: 1593
目前同步到主库数据库文件位置:1593
在主库上进入mysql,并查看主库状态
mysql> show master statusG;
*************************** 1. row ***************************
             File: master-18-69.000001
         Position: 1593
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-7,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-23
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

同样主库文件最后位置也是1593,说明从库同步设置完毕

 

 
 
原文地址:https://www.cnblogs.com/sky-cheng/p/12030598.html