Mysql主从复制

2019-10-29  本文已影响0人  iDevOps
Mysql Replication
常见方案
Mysql主从

我这里准备了两台主机
192.168.5.133(主)
192.168.5.132(从)

1.修改配置文件/etc/my.cnf

log-bin=mysql-bin-master # 启用二进制日志
server-id=1  # 本机数据库id
binlog-do-db=cms  # 定义可以被服务器复制的库,二进制需要同步的数据库名
binlog-ignore-db=mysql # 不可以被从服务器复制的库

2.重启mysql

systemctl restart mariadb.service

3.授权

# 登陆mysql
[root@centos7-app etc]# mysql -uroot -p123456
# 授权
MariaDB [(none)]> grant replication slave on *.* to slave@192.168.5.132 identified by "123456";
Query OK, 0 rows affected (0.00 sec)
# 查看状态信息
MariaDB [(none)]> show master status;
+-------------------------+----------+--------------+------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000001 |      396 | cms          | mysql            |
+-------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
# 查看二进制信息
MariaDB [(none)]> show binlog events \G
*************************** 1. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 245
       Info: Server ver: 5.5.64-MariaDB, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 245
 Event_type: Query
  Server_id: 1
End_log_pos: 396
       Info: grant replication slave on *.* to slave@192.168.5.132 identified by "123456"
2 rows in set (0.00 sec)

# 二进制日志
[root@centos7-app etc]# ll /var/lib/mysql/
省略...
-rw-rw----. 1 mysql mysql      396 10月 29 14:22 mysql-bin-master.000001
-rw-rw----. 1 mysql mysql       26 10月 29 14:18 mysql-bin-master.index
省略...

4.导出数据库传给从服务器

mysqldump -uroot -p123456 cms > cms.sql

1.先查看下数据库版本是否一致

MariaDB [test]> show variables like '%version%';
+-------------------------+----------------------+
| Variable_name           | Value                |
+-------------------------+----------------------+
| innodb_version          | 5.5.61-MariaDB-38.13 |
| protocol_version        | 10                   |
| slave_type_conversions  |                      |
| version                 | 5.5.64-MariaDB       |
| version_comment         | MariaDB Server       |
| version_compile_machine | x86_64               |
| version_compile_os      | Linux                |
+-------------------------+----------------------+
7 rows in set (0.00 sec)

2.测试连接主服务器是否成功

[root@centos7-1 ~]# mysql -uslave -p123456 -h 192.168.5.133
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.01 sec)

只有复制权限,看不到其他库。属于正常。

3.修改从服务器配置etc/my.cnf

#从服务器ID号,不要和主ID相同 ,如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,
#必须与主服务器的以及其它从服务器的不相同。可以认为server-id值类似于IP地址:这些ID值能唯一识别
#复制服务器群集中的每个服务器实例
server-id=2 

4.重启数据库

systemctl restart mariadb.service

5.设置和主数据库服务器同步

# 停止slave
MariaDB [(none)]> start slave;
# 设置主数据库服务器并连接
MariaDB [(none)]> change master to master_host='192.168.5.133',master_user='slave',master_password='123456';
# 启动slave
MariaDB [(none)]> start slave;
# 查看状态
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.133
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 396
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 687
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes   # 负责与主机进行io通信
            Slave_SQL_Running: Yes   # 负责自己的slave mysql进程,这两个只要是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: 396
              Relay_Log_Space: 983
              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
1 row in set (0.00 sec)

6.导入数据

mysql -uroot -p123456 cms < cms.sql

7.查看主服务器状态

MariaDB [cms]> show processlist \G
*************************** 1. row ***************************
      Id: 3
    User: root
    Host: localhost
      db: cms
 Command: Query
    Time: 0
   State: NULL
    Info: show processlist
Progress: 0.000
*************************** 2. row ***************************
      Id: 5
    User: slave
    Host: 192.168.5.132:54872
      db: NULL
 Command: Binlog Dump
    Time: 39
   State: Master has sent all binlog to slave; waiting for binlog to be updated
    Info: NULL
Progress: 0.000
2 rows in set (0.00 sec)

接下来就可以在主服务器上插入数据进行测试了....

上一篇下一篇

猜你喜欢

热点阅读