centos7 MySQL主从同步

准备

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)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值