mysql主从

放肆的年华 提交于 2020-01-01 01:30:23

1.mysql简介

在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。

想几个问题:

  • 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
  • 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?

1.1 主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务(这里的备份不是指数据库的备份,而是服务器的备份,相当于一台备用服务器)

    1.2 主从形式

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

    2. 主从复制原理


    主从复制步骤:
  • 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
  • 从库生成两个线程,一个I/O线程,一个SQL线程
    • I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
    • SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

      3. 主从复制配置

主从复制配置步骤:

1.确保从数据库与主数据库里的数据一样

2.在主数据库里创建一个同步账号授权给从数据库使用

3.配置主数据库(修改配置文件)

4.配置从数据库(修改配置文件)

需求:

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

环境说明:

数据库角色 IP 应用与系统版本 有无数据
主数据库 192.168.153.152 centos7/redhat7
mysql-5.7
无数据到有数据
从数据库 192.168.153.162 centos7/redhat7
mysql-5.7
无数据
从数据库 192.168.153.164 centos7/redhat7
mysql-5.7
无数据

3.1 mysql安装

[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
[root@localhost ~]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64"
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql*
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 35 5月  15 23:26 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64
[root@localhost local]# ls
bin  games    lib    libexec  mysql-5.7.22-linux-glibc2.12-x86_64  share
etc  include  lib64  mysql    sbin                                 src
[root@localhost local]# cd
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh 
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-05-15T15:33:17.059360Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-15T15:33:17.309490Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-05-15T15:33:17.634883Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-15T15:33:17.910710Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c33692ba-7726-11e9-8825-000c29c97322.
2019-05-15T15:33:17.911242Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-05-15T15:33:17.911859Z 1 [Note] A temporary password is generated for root@localhost: %l.lUhAhl4;n
[root@localhost ~]# echo '%l.lUhAhl4;n' > pass
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# ss -antl
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                  :::*                       

3.2 mysql主从配置

3.2.1 确保从数据库与主数据库里的数据一样(两个里面都无数据)

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

mysql> set password=password('ly123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> create user 'repl'@'192.168.153.162' identified by 'repl123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.153.162';
Query OK, 0 rows affected (0.00 sec)

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

mysql> quit

3.2.3 配置主数据库

[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin   //启用binlog日志
server-id=1     //数据库服务器唯一标识符,主库的server-id值必须比从库的小

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 


//重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost ~]# ss -antl
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                  :::*  
//查看主库的状态
[root@localhost ~]# mysql -uroot -ply123
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 2
Server version: 5.7.22-log 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.4 配置从数据库

[root@localhost ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2     //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
relay-log=mysql-relay-bin       //启用中继日志relay-log

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


//重启从库的mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost ~]# ss -antl
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.153.152',master_user='repl',mastter_password='repl123',master_log_file='mysql-bin.000001',master_log_pos=154;;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看从服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.153.152
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             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: 527
              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: eec564cd-7744-11e9-8dfe-000c29c97322
             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)

3.2.5 测试验证

在主服务器的liucongrong库的student表中插入数据

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

mysql> use liucongrong;
Database changed
mysql> create table student(name varchar(100),age tinyint);
Query OK, 0 rows affected (0.02 sec)

mysql> insert student values ('tom',29),('jerry',20);
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+-------+------+
| name  | age  |
+-------+------+
| tom   |   29 |
| jerry |   20 |
+-------+------+
2 rows in set (0.00 sec)

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

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

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

mysql> select * from liucongrong.student;
+-------+------+
| name  | age  |
+-------+------+
| tom   |   29 |
| jerry |   20 |
+-------+------+
2 rows in set (0.00 sec)

4.mysql主从配置

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

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//先查看主库有哪些库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liucongrong        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
//再查看从库有哪些库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此锁表的终端必须在备份完成以后才能退出

//备份主库并将备份文件传送到从库
[root@localhost ~]# mysqldump -uroot -ply123 --all-databases > /root/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
all.sql
anaconda-ks.cfg
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
pass
[root@localhost ~]# scp /root/all.sql root@192.168.153.164:/root/
The authenticity of host '192.168.153.164 (192.168.153.164)' can't be established.
ECDSA key fingerprint is SHA256:NoDuGXDlaqriTX1RFynwCDbRXu2QrfUpNwZyglaGUdI.
ECDSA key fingerprint is MD5:10:35:ff:d9:f7:cd:8a:0a:8d:63:72:11:29:28:d3:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.153.164' (ECDSA) to the list of known hosts.
root@192.168.153.164's password: 
Permission denied, please try again.
root@192.168.153.164's password: 
a 100%  784KB  50.3MB/s   00:00

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

//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@localhost ~]# mysql -uroot -ply123 < /root/all.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -ply123 
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> select * from liucongrong.student;
+-------+------+
| name  | age  |
+-------+------+
| tom   |   29 |
| jerry |   20 |
+-------+------+
2 rows in set (0.00 sec)

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

mysql> CREATE USER 'repl'@'192.168.153.164' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.01 sec)

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

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

4.1.3配置主数据库

[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin   //启用binlog日志
server-id=1     //数据库服务器唯一标识符,主库的server-id值必须比从库的小

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 


//重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost ~]# ss -antl
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                  :::*  
//查看主库的状态
[root@localhost ~]# mysql -uroot -ply123
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 2
Server version: 5.7.22-log 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.1.3 配置从数据库

[root@localhost ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=4    //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin       //启用中继日志relay-log

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


//重启从库的mysql服务
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -antl
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.153.152',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='repl123',
    -> MASTER_LOG_FILE='mysql-bin.000004',
    -> MASTER_LOG_POS=1443;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.153.152
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1443
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             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: 1443
              Relay_Log_Space: 527
              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: eec564cd-7744-11e9-8dfe-000c29c97322
             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)

4.1.4测试验证

在主库插入数据

mysql> insert student values ('zhangsan',20),('lisi',18);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----------+------+
| name     | age  |
+----------+------+
| tom      |   29 |
| jerry    |   20 |
| zhangsan |   20 |
| lisi     |   18 |
+----------+------+
4 rows in set (0.00 sec)

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

                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> select * from liucongrong.student;
+----------+------+
| name     | age  |
+----------+------+
| tom      |   29 |
| jerry    |   20 |
| zhangsan |   20 |
| lisi     |   18 |
+----------+------+
4 rows in set (0.00 sec)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!