centos7 MySQL主从同步

為{幸葍}努か 提交于 2019-12-26 12:30:55

准备

MySQL主从同步需要两个系统,因此在VM虚拟机中创建两个cento7虚拟机,IP分别为 192.168.1.11和192.168.1.12。
因为平常主要使用12的为主,因此:主服务器为(192.168.1.12) 从服务器为(192.168.1.11)

配置主服务器

登录MySQL交互模式创建用于同步的用户

[root@bogon ~]# mysql -uroot -p
mysql> create user repl;
mysql>grant replication slave on *.* to 'bo'@'192.168.*.***' identified by '123123';
mysql> flush privileges;

查看主服务器的配置
mysql> show master status;
在这里插入图片描述
进入MySQL配置文件 /etc/my.cnf

在[mysqld]内容中添加
server-id=12
保存并退出
重启MySQL:[root@bogon ~]# systemctl restart mysql

备注:
在配置重启后不成功并报错
在这里插入图片描述
解决办法
在 /etc/my.cnf配置server时已经存在server-id=1的选项,删除此项。保存退出并重启MySQL服务

配置从服务器

进入MySQL配置文件 /etc/my.cnf

在[mysqld]内容中添加
server-id=22
保存并退出
重启MySQL:[root@bogon ~]# systemctl restart mysql

登录MySQL交互模式

mysql -u root -p
进入交互模式后输入以下命令:
mysql> stop slave;
mysql> change master to
         -> master_host='192.168.1.12',
         -> master_user='bo',
         -> master_password='123123',
          -> master_log_file='mysql-bin.000011',
          -> master_log_pos=107;

mysql>start slave;     在输入以上命令都正确的情况下开启slave;

查看slave服务器状态:
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.12
                  Master_User: bo
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 107
               Relay_Log_File: bogon-relay-bin.000003
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000011
             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: 107
              Relay_Log_Space: 409
              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: 12
1 row in set (0.00 sec)

在这里插入图片描述

show slave status \G 的详解:

验证是否可以同步

在主服务器端

输入命令:
mysql> create database iii;
Query OK, 1 row affected (0.00 sec)
显示界面:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| iii                |
| mysql              |
| ooo                |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
7 rows in set (0.00 sec)

在从服务器端

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| iii                |
| mysql              |
| ooo                |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
7 rows in set (0.00 sec)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!