Mysql 主从复制

Mysql 主从复制 

主数据库服务器:192.168.100.61
从数据库服务器:192.168.100.62

一:主服务器master(192.168.100.61)操作

1.开启二进制日志
[mysql@rac1 ~]# vi /etc/my.cnf 
log-bin=mysql-bin 
server-id=1 

重启mysql生效 
[mysql@rac1 ~]# service mysqld restart 

2.创建用户并授权
mysql> CREATE USER 'repl'@'192.168.100.62' IDENTIFIED BY '123';
Query OK, 0 rows affected (0.46 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.100.62';
Query OK, 0 rows affected (0.09 sec)

mysql> flush privileges; 
Query OK, 0 rows affected (0.00 sec)

---从机上可以验证新建的用户是否可以登录
---[mysql@rac2 bin]$ ./mysql -urep1 -h 192.168.100.61 -p123

3.查看master状态
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      773 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

二、从服务器slave(192.168.100.62)操作

1.修改my.cnf文件
[root@rac2 etc]# vi my.cnf 
server-id=2

重启mysql生效 
[mysql@rac2 ~]# service mysqld restart 

2.配置同步 
[mysql@rac2 bin]$ ./mysql -uroot -p123
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 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> CHANGE MASTER TO
    -> MASTER_HOST='192.168.100.61',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='123',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=773;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

3.启动Slave同步
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)

4.查看状态 
确保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.100.61
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 773
               Relay_Log_File: rac2-relay-bin.000002
                Relay_Log_Pos: 320
        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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 773
              Relay_Log_Space: 526
              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: 1
                  Master_UUID: 875c0a47-67aa-11e8-be45-000c29a6769f
             Master_Info_File: /usr/local/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: 
            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

三:数据同步验证

1.主服务器生成测试数据
mysql> create database chenjch;
Query OK, 1 row affected (0.08 sec)

mysql> use chenjch;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.33 sec)

mysql> insert into t1(id) values(1);
Query OK, 1 row affected (0.44 sec)

2.从服务器检查数据是否同步
mysql> use chenjch;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_chenjch |
+-------------------+
| t1                |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.01 sec)
请使用浏览器的分享功能分享到微信等