• mySQL主从


    mySQL主从

    主从简介

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

    想几个问题:

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

    主从作用

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

    主从形式

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

    主从复制原理

    主从复制步骤:

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

    主从复制配置

    主从复制配置步骤:

    1. 确保从数据库与主数据库里的数据一样
    2. 在主数据库里创建一个同步账号授权给从数据库使用
    3. 配置主数据库(修改配置文件)
    4. 配置从数据库(修改配置文件)
      需求:
      搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

    环境说明:

    数据库角色 IP 应用与系统版本 有无数据
    主数据库 172.16.12.128 centos7/redhat7
    mysql-5.7
    有数据
    从数据库 172.16.12.129 centos7/redhat7
    mysql-5.7
    无数据

    mysql安装

    分别在主从两台服务器上安装mysql-5.7版本

    mysql主从配置

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

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

    //先查看主库有哪些库
    [root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | student            |
    | sys                |
    | teacher            |
    +--------------------+
    
    //再查看从库有哪些库
    [root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    
    
    //全备主库
    //全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
    mysql> FLUSH TABLES WITH READ LOCK;
    Query OK, 0 rows affected (0.00 sec)
    //此锁表的终端必须在备份完成以后才能退出
    
    //备份主库并将备份文件传送到从库
    [root@localhost ~]# mysqldump -uroot -pwangqing123! --all-databases > /opt/all-201808191200.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@localhost ~]# ls /opt/
    all-201808191200.sql
    [root@localhost ~]# scp /opt/all-201808191200.sql root@172.16.12.129:/opt/
    root@172.16.12.129's password:
    all-201808191200.sql                              100%  786KB  10.6MB/s   00:00 
    
    //解除主库的锁表状态,直接退出交互式界面即可
    mysql> quit
    Bye
    
    
    //在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
    [root@localhost ~]# mysql -uroot -pwangqing123! < /opt/all-201808191200.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    [root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | student            |
    | sys                |
    | teacher            |
    +--------------------+
    

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

    在主上授权给从
    
    MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.23.133' identified by 'repl123'; 
    Query OK, 0 rows affected (0.000 sec)
    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.000 sec)
    
    在从上链接主
    
    [root@localhost ~]# mysql -urepl -prepl123 -h192.168.23.132
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 9
    Server version: 10.3.17-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MariaDB [(none)]> 
    

    配置主数据库

    [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=10 //数据库服务器唯一标识符,主库的server-id值必须比从库的大
    
    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                               :::* 
    
    //查看主库的状态
    MariaDB [(none)]> show master status;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 |      328 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.000 sec)
    

    配置从数据库

    [root@localhost ~]# vim /etc/my.cnf
    //添加如下内容
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    server-id=20  //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
    relay-log=myrelay       //启用中继日志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                                    :::*
    
    //配置并启动主从复制
    
    MariaDB [(none)]> change master to master_host='192.168.23.132',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000001',master_log_pos=328;
    Query OK, 0 rows affected (0.003 sec)
    
    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [(none)]> show slave statusG;
    *************************** 1. row ***************************
                    Slave_IO_State: Waiting for master to send event
                       Master_Host: 192.168.23.132
                       Master_User: repl
                       Master_Port: 3306
                     Connect_Retry: 60
                   Master_Log_File: mysql-bin.000002
               Read_Master_Log_Pos: 328
                    Relay_Log_File: mariadb-relay-bin.000003
                     Relay_Log_Pos: 627
             Relay_Master_Log_File: mysql-bin.000002
                  Slave_IO_Running: Yes
                 Slave_SQL_Running: Yes
    

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

    MariaDB [school]> select * from student;
    +----+------+------+-------+
    | id | name | age  | score |
    +----+------+------+-------+
    |  1 | aaa  |   14 |    80 |
    |  2 | bbb  |   15 |    76 |
    |  3 | ccc  |   16 |    88 |
    |  4 | ddd  |   17 |    99 |
    +----+------+------+-------+
    4 rows in set (0.000 sec)
    
    MariaDB [(none)]> select * from school.student;
    +----+------+------+-------+
    | id | name | age  | score |
    +----+------+------+-------+
    |  1 | aaa  |   14 |    80 |
    |  2 | bbb  |   15 |    76 |
    |  3 | ccc  |   16 |    88 |
    |  4 | ddd  |   17 |    99 |
    +----+------+------+-------+
    4 rows in set (0.000 sec)
    
  • 相关阅读:
    jquery 根据 option 的 text 定位选中 option
    Mac 打开任务管理器 关闭程序
    什么是 IaaS、PaaS、SaaS
    网站 A/B Test
    PHP 设计模式之策略模式
    mybatis-plus的使用 ------ 入门
    IEDA和svn上同步及更新代码【我】
    springBoot 项目测试【我】
    Idea检出项目配置【我】
    IDEA常用的风格设置
  • 原文地址:https://www.cnblogs.com/Ycqifei/p/14224541.html
Copyright © 2020-2023  润新知