MySQL搭建主从(一主一从)

原理:将主结点中binlog日志内容,实时传送到从结点relay log中,再由SQL线程解析执行改变从结点中的数据,达到主从数据一致;

应用场景:读写分离、数据备份等;

环境介绍:

MySQL版本:8.0.28 MySQL Community Server - GPL

操作系统:CentOS 7

1、规划

主机 IP 类型
mysql8_1 172.17.0.12
mysql8_2 172.17.0.13

2、安装MySQL,主从上都执行如下2命令,等待完成即可;

主、从:

rpm -ivh https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm
yum install -y mysql-community-server-8.0.28

3、配置my.cnf,在[mysqld]下添加如下配置,数字必须不同。

主:

server-id = 1

从:

server-id = 2

4、启动数据库,主从都执行;

主、从:

systemctl start mysql

5、添加同步账号并授权,此处用户密码为slave_sync1/123456;

主:

mysql> CREATE USER 'slave_sync1'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.03 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_sync1'@'%';
Query OK, 0 rows affected (0.00 sec)

6、配置同步;

主:

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      674 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主结点中当前binlog文件及其位置

从:

mysql> CHANGE MASTER TO MASTER_HOST='172.17.0.12', MASTER_USER='slave_sync1', MASTER_PASSWORD='123456', MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=674;
Query OK, 0 rows affected, 8 warnings (0.06 sec)

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

CHANGE MASTER命令中均来自“主”,IP、用户、密码为自定义;

MASTER_LOG_FILE='binlog.000002' # 同步从哪个binlog文件开始同步,见上方“show master status;”中内容;

MASTER_LOG_POS=674 # 表示从binlog文件什么位置开始同步,见上方“show master status;”;

 7、检查同步状态,Slave_IO_Running和Slave_SQL_Running必须都是Yes,才正常;

从:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.17.0.12
                  Master_User: slave_sync1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 674
               Relay_Log_File: mysql8_2-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 

8、验证主从同步功能,在主结点建库建表,从结点均能查询到,如下;

主:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.06 sec)

mysql> create database test_sync;
Query OK, 1 row affected (0.02 sec)

mysql> use test_sync
Database changed

mysql> create table books(id int, name varchar(50));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into books values(1, "MySQL");
Query OK, 1 row affected (0.03 sec)

mysql> insert into books values(2, "Java");
Query OK, 1 row affected (0.01 sec)

mysql> insert into books values(3, "C++");
Query OK, 1 row affected (0.00 sec)

mysql> show tables;
+---------------------+
| Tables_in_test_sync |
+---------------------+
| books               |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from books;
+------+-------+
| id   | name  |
+------+-------+
|    1 | MySQL |
|    2 | Java  |
|    3 | C++   |
+------+-------+
3 rows in set (0.00 sec)

从:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_sync          |
+--------------------+
5 rows in set (0.01 sec)

mysql> use test_sync;
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_sync |
+---------------------+
| books               |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from books;
+------+-------+
| id   | name  |
+------+-------+
|    1 | MySQL |
|    2 | Java  |
|    3 | C++   |
+------+-------+
3 rows in set (0.00 sec)

你可能感兴趣的:(MySQL,mysql,数据库)