mysql主从配置实现一主一从读写分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从配置实现一主一从读写分离相关的知识,希望对你有一定的参考价值。

主从介绍

定义

  • mysql主从又叫Replication、AB复制。简单讲就是A与B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,实现数据实时同步
  • mysql主从是基于binlog,主上需开启binlog才能进行主从
  • 主从过程大概有3个步骤

    • 主将更改操作记录到binlog里
    • 从将主的binlog事件(sql语句) 同步本机上并记录在relaylog里
    • 从根据relaylog里面的sql语句按顺序执行
  • 主上有一个log dump线程,用来和从的I/O 线程传递binlog
  • 从上有两个线程,其中 I/O线程用来同步主的binlog里面的sql语句落地

    主从的应用场景:

    数据的高可用。(主:作为读写数据,从:实时同步,当主宕机时,从 也可以及时提供服务)

数据的负载均衡。(客户从 从 这台机器上读取数据(但是不

主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务

    主从形式

    技术分享图片

  • 一主一从
  • 主主复制
  • 一主多从---扩展系统读取的性能,因为读是在从库读取的
  • 多主一从---5.7版本开始支持
  • 联级复制

    主从复制原理

    技术分享图片

主从复制步骤

  • 主库将所有的写操作记录在binlog日志中,并生成log dump线程,将binlog日志传给从库的I/O线程
  • 从库生成两个线程,一个是I/O线程,另一个是SQL线程

    • I/O线程去请求主库的binlog日志,并将binlog日志中的文件写入relay log(中继日志)中
    • SQL线程会读取relay loy中的内容,并解析成具体的操作,来实现主从的操作一致,达到最终数据一致的目的

      主从复制配置

      主从复制配置步骤:

      1. 确保从数据库与主数据库里的数据一致
      2. 在主数据库里创建一个同步账户授权给从数据库使用
      3. 配合主数据库(修改配置文件)
        4.配置从数据库(修改配置文件)

        需求

        搭建两台MYSQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

        环境说明

数据库角色 IP 应用与系统 有无数据
主数据库 192.168.24.18 centos7 mysql-5.7
从数据库 192.168.24.129 centos7 mysql-5.7

在两台服务器上都按装mysql

具体配置查看《mysql进阶简单解析》

mysql主从配置

确保从数据库与主数据库的数据一样

先在主数据库创建所需要同步的库和表

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create database doudou;
Query OK, 1 row affected (0.03 sec)

mysql> create database job;
Query OK, 1 row affected (0.00 sec)

mysql> create database mary;
Query OK, 1 row affected (0.00 sec)

mysql> use doudou;
Database changed
mysql> create table tom (id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.22 sec)
mysql> insert tom(id,name,age) values(1,"lisi",6),(2,"waangwu",5),(3,"zhaotie",9);
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tom;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | lisi    |    6 |
|  2 | waangwu |    5 |
|  3 | zhaotie |    9 |
+----+---------+------+
3 rows in set (0.01 sec)

mysql>                   

备份主库
备份主库时需要另开一个终端,给数据库上读锁,避免在备份期间有其他人在写入导致数据同步的不一致

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> flush tables with read lock;
Query OK, 0 rows affected (0.17 sec)

mysql> //此锁表的终端必须在备份完成以后才能退出(退出锁表失效)                            

备份主库并将备份文件传送到从库

[[email protected] ~]# mysqldump -uroot -plinfan123 --all-databases >/opt/all-20180907.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ls /opt/
all-20180907.sql  data
[[email protected] ~]# scp /opt/all-20180907.sql [email protected]:/opt/
The authenticity of host ‘192.168.24.130 (192.168.24.130)‘ can‘t be established.
ECDSA key fingerprint is SHA256:w+sgREnQRuhBiqS0qL9wlAImCSmvSQ6KnNqW6N3znJ0.
ECDSA key fingerprint is MD5:f0:fd:ea:c7:97:83:f0:b0:03:84:d2:a6:0a:23:12:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.24.130‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
all-20180907.sql                                                                        100%

解除主库的锁表状态,直接退出交互式界面即可


mysql> quit
Bye

在从库上恢复主库的备份并查看是否与主库的数据保持一致

[[email protected] ~]# mysql -uroot -plinfan123 < /opt/all-20180907.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -plinfan123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| doudou             |
| job                |
| mary               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.01 sec)

mysql> use doudou
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> select * from tom;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | lisi    |    6 |
|  2 | waangwu |    5 |
|  3 | zhaotie |    9 |
+----+---------+------+
3 rows in set (0.00 sec)

在主数据库创建一个同步账户授权给从数据使用

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create user ‘repl‘@‘192.168.24.130‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.04 sec)

mysql> grant replication slave on *.* to ‘repl‘@‘192.168.24.130‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

配置主数据库

编辑配置文件

[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//添加以下内容
log-bin=mysql-bin //启用binlog日志
server-id=1 //主数据库服务器唯一标识符 主的必须必从大
log-error=/opt/data/mysql.log

重启mysql服务

[[email protected] ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.Logging to ‘/opt/data/mysql.log‘.
.......... SUCCESS! 
[[email protected] ~]# ss -natl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port
LISTEN     0      128                               *:22                                            *:*
LISTEN     0      100                       127.0.0.1:25                                            *:*
LISTEN     0      128                              :::22                                           :::*
LISTEN     0      100                             ::1:25                                           :::*
LISTEN     0      80                               :::3306            

查看主库的状态(里面的数据要记住,待会会用到)

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

配置从数据库

编辑配置文件

[[email protected] ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//添加以下内容:
server-id=2 //设置从库的唯一标识符 从的必须比主小
relay-log=mysql-relay-bin //启用中继日志relay log
error-log=/opt/data/mysql.log

重启从库的mysql服务

[[email protected] ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.Logging to ‘/opt/data/mysql.log‘.
.......... SUCCESS! 
[[email protected] ~]# ss -natl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port
LISTEN     0      128                               *:22                                            *:*
LISTEN     0      100                       127.0.0.1:25                                            *:*
LISTEN     0      128                              :::22                                           :::*
LISTEN     0      100                             ::1:25                                           :::*
LISTEN     0      80                               :::3306    

配置并启动主从复制

mysql> change master to
    -> master_host=‘192.168.24.128‘,
    -> master_user=‘repl‘,
    -> master_password=‘123456‘,
    -> master_log_file=‘mysql-bin.000001‘,
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

查看从服务器状态


mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.24.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: linfan-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes //此处必须为yes
            Slave_SQL_Running: Yes
            //此处必须为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: 154
              Relay_Log_Space: 528
              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: 1
                  Master_UUID: 3396fe35-b1e1-11e8-81bb-000c292340f6
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec) 

测试验证

在主服务器的doudou库的tom表插入数据:

mysql> use doudou;
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> select * from tom;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | lisi    |    6 |
|  2 | waangwu |    5 |
|  3 | zhaotie |    9 |
+----+---------+------+
3 rows in set (0.00 sec)

mysql> insert tom(id,name,age) value (4,"mary",18);
Query OK, 1 row affected (0.10 sec)

mysql> select * from tom;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | lisi    |    6 |
|  2 | waangwu |    5 |
|  3 | zhaotie |    9 |
|  4 | mary    |   18 |
+----+---------+------+
4 rows in set (0.00 sec)

在从数据库查看是否数据同步

mysql> use doudou;
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> select * from tom;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | lisi    |    6 |
|  2 | waangwu |    5 |
|  3 | zhaotie |    9 |
|  4 | mary    |   18 |
+----+---------+------+
4 rows in set (0.00 sec)

以上是关于mysql主从配置实现一主一从读写分离的主要内容,如果未能解决你的问题,请参考以下文章

mysql主从配置实现一主一从读写分离

mysql主从配置实现一主一从读写分离

3读写分离---一主一从(mycat)

Mycat中间件实现一主一从和双主双从的读写分离

实现mysql主从

MySQL主从复制实现