加入收藏 | 设为首页 | 会员中心 | 我要投稿 武汉站长网 (https://www.027zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

CentOs7中mysql5.7如何做成主从复制配置

发布时间:2021-12-24 13:15:27 所属栏目:MySql教程 来源:未知
导读:这篇文章主要介绍CentOs7中mysql5.7如何实现主从复制配置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! MySQL5.7主、从配置具体步骤: 点击(此处)折叠或打开 1.两台MySQL服务器 主:192.168.253.138 端口:1221 从:192.168.253.1
这篇文章主要介绍CentOs7中mysql5.7如何实现主从复制配置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
 
MySQL5.7主、从配置具体步骤:
 
点击(此处)折叠或打开
 
1.两台MySQL服务器
主:192.168.253.138  端口:1221
从:192.168.253.139  端口:1221
2.修改主从配置参数
2.1 主服务器
##添加入下内容
vi /etc/my.cnf
[mysqld]
log-bin = mysql-bin ##必须启用二进制格式日志
server-id=138  ##server-id必须唯一,一般为IP的尾数
2.2 从服务器
##添加入下内容
vi /etc/my.cnf
[mysqld]
log-bin = mysql-bin ##启用二进制格式日志,可选配置
server-id=139  ##server-id必须唯一,一般为IP的尾数
3.重启主、从服务器使修改生效
主服务器(138)
[root@my01 ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
从服务器(139)
[root@my02 ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
4.在主服务器上创建复制账号并授权slave
[root@my01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.19-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> grant replication slave on *.* to 'mysync'@'%' identified by 'mysql123';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>
5.查看主服务器状态
##记录File与Position的值,配置从服务器时使用,之后主服务器就不要做任何操作了,避免值的改变使得从服务器配置失败
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1306 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
 
Query OK, 0 rows affected (0.01 sec)
7.查看从服务器复制状态
##最主要的是查看Slave_IO_Running: Yes,Slave_SQL_Running: Yes 这两项是不是YES,是则代表主从配置成功,否则失败
mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.253.138
                  Master_User: mysync
                  Master_Port: 1221
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1306
               Relay_Log_File: my02-relay-bin.000003
                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:
        
1 row in set (0.00 sec)
mysql>
8.测试主、从情况
##主服务器
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database test;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table t(id int,name varchar(10));
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t values (1,"zhang san");
Query OK, 1 row affected (0.07 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t |
+----------------+
1 row in set (0.00 sec)
mysql> select * from
    -> t;
+------+-----------+
| id | name |
+------+-----------+
| 1 | zhang san |
+------+-----------+
1 row in set (0.00 sec)
mysql> insert into t values (2,"li si");
Query OK, 1 row affected (0.01 sec)
mysql>
###从服务器
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t;
+------+-----------+
| id | name |
+------+-----------+
| 1 | zhang san |
| 2 | li si |
+------+-----------+
2 rows in set (0.00 sec)
mysql>
####恭喜成功了!!!
##注意:主服务器权限配置,如下:
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
###此配置保证其它服务器能够连接到主服务器,否则后面的从服务器配置复制时会失败!!!
以上是“CentOs7中mysql5.7如何实现主从复制配置”这篇文章的所有内容,感谢各位的阅读!

(编辑:武汉站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读