Mysql主从复制

目录

一、克隆一台虚拟机

二、主库配置

 1、配置server-id并开启binlog功能

 2、重启数据库  

3、创建用户

 4、授权用户

 5、查看用户

 6、查看master状态

三、从库配置

1、配置server-id

2、配置主库同步

3、启动slave

4、查看slave状态

四、检测


一、克隆一台虚拟机

配置信息:主库IP地址为192.168.10.129,mysql版本为8.0.26

                  克隆出来的从库IP地址为192.168.10.134 (输入ifconfig可以查看网卡的IP地址)

二、主库配置

 1、配置server-id并开启binlog功能

[root@localhost ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
log-bin=mysql-bin
server-id=120

 2、重启数据库  

[root@localhost ~]# systemctl restart mysqld.service 
[root@localhost ~]# mysql -uroot -p
输入密码

3、创建用户

        创建用户opl,密码为123456

mysql> create user 'opl'@'192.168.10.%' identified with mysql_native_password by'123456';
Query OK, 0 rows affected (0.01 sec)

 4、授权用户

mysql> grant replication slave on *.* to 'opl'@'192.168.10.%';

 5、查看用户

mysql> show grants for 'opl'@'192.168.10.%';
+--------------------------------------------------------+
| Grants for opl@192.168.10.%                            |
+--------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `opl`@`192.168.10.%` |
+--------------------------------------------------------+
1 row in set (0.00 sec)

 6、查看master状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      671 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

三、从库配置

1、配置server-id

不要跟主库的server-id一样就行

[root@localhost ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
server_id=130   

2、配置主库同步

mysql> change master to
    -> master_host='192.168.10.129',    #主库的IP地址
    -> master_port=3306,        
    -> master_user=opl,        #用户名
    -> master_password='123456',    #用户密码
    -> master_log_file='mysql-bin.000006',    #主库的二进制文件名
    -> master_log_pos=671;    #主库文件的pos

3、启动slave

mysql> start slave;

4、查看slave状态

mysql> show slave status\G

显示的信息会出现问题:

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

原因:因为克隆的虚拟机,uuid会一样

修改从库的uuid如下:

[root@localhost ~]# vim /var/lib/mysql/auto.cnf
server-uuid=b71851c8-2ccc-11ed-9eb2-000c298072a7    #随便改下就行,比如末尾的7改为9

再次查看

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

mysql> show slave status\G

部分信息显示如下:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.10.129
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 671
               Relay_Log_File: localhost-relay-bin.000010
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

四、检测

在主库的mysql创建一个不存在的数据库 

mysql> create database yup;

在从库的数据库查看数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| yup                |
+--------------------+
6 rows in set (0.00 sec)

发现从库上也有yup的数据库,完成。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

维运

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值