zoukankan      html  css  js  c++  java
  • mysql主从

    1. 主从简介

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

    想几个问题:

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

    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. 主从复制配置

    3.1主从复制配置步骤:

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

    3.2 mysql主从配置

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

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

    
    //先查看主库有哪些库
    [root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    +--------------------+
    
    | Database |
    
    +--------------------+
    
    | information_schema |
    
    | mysql |
    
    | performance_schema |
    
    | sys |
    
    +--------------------+
    
    
    
    //再查看从库有哪些库
    [root@localhost ~]# mysql -uroot -p123456 -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.14 sec)
    
    //此锁表的终端必须在备份完成以后才能退出
    
    //备份主库并将备份文件传送到从库
    [root@localhost ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-20191224.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    
    [root@localhost ~]# ls /opt/
    
    all-20191224.sql data
    
    [root@localhost ~]# scp /opt/all-20191224.sql root@192.168.136.145:/opt/
    
    root@192.168.136.145's password: 
    
    all-20191224.sql 100% 782KB 2.7MB/s 00:00  
    
    
    
    //解除主库的锁表状态,直接退出交互式界面即可
    mysql> quit
    Bye
    
    
    
    //在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
    [root@localhost ~]# mysql -uroot -p123456 < /opt/all-20191224.sql 
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    [root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
    
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    +--------------------+
    
    | Database |
    
    +--------------------+
    
    | information_schema |
    
    | mysql |
    
    | performance_schema |
    
    | sys |
    
    +--------------------+
    
    

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

    mysql> CREATE USER 'repl'@'192.168.136.145' IDENTIFIED BY '123456';
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.136.145';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    

    3.2.3 配置主数据库

    [root@localhost ~]# vim /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   
    server-id=1 
    
    //重启mysql服务
    [root@localhost ~]# vim /etc/my.cnf
    [root@localhost ~]# service 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> 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]
    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
    
    //重启从库的mysql服务
    [root@localhost ~]# vim /etc/my.cnf
    [root@localhost ~]# service 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 *:10050 *:*                  
    LISTEN 0 25 *:514 *:*                  
    LISTEN 0 128 :::22 :::*                  
    LISTEN 0 100 ::1:25 :::*                  
    LISTEN 0 25 :::514 :::*                  
    LISTEN 0 80 :::3306 :::*      
    
    //配置并启动主从复制
    mysql> CHANGE MASTER TO
        -> MASTER_HOST='192.168.136.144',
        -> MASTER_USER='repl',
        -> MASTER_PASSWORD='123456',
        -> MASTER_LOG_FILE='mysql-bin.000001',
        -> MASTER_LOG_POS=154;
    Query OK, 0 rows affected, 2 warnings (0.05 sec)
    
    mysql> start slave;
    Query OK, 0 rows affected (0.04 sec)
    
    //查看从服务器状态
    mysql> show slave status G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.136.144
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 154
                   Relay_Log_File: mysql-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: 527
                  Until_Condition: None
                   Until_Log_File: 
    

    3.2.5 测试验证

    在主服务器的student库的student表中插入数据:
    
    mysql> create database student;
    
    Query OK, 1 row affected (0.03 sec)
    
    
    
    mysql> use student;
    
    Database changed
    
    mysql> create table student (id int not null,name varchar(100) not null,age tinyint);
    
    Query OK, 0 rows affected (0.09 sec)
    
    
    
    mysql> insert into student (id,name,age) value (1,'tom',20);
    
    Query OK, 1 row affected (0.16 sec)
    
    
    
    mysql> select * from student;
    
    +----+------+------+
    
    | id | name | age |
    
    +----+------+------+
    
    | 1 | tom | 20 |
    
    +----+------+------+
    
    1 row in set (0.00 sec)
    
    
    
    在从数据库中查看数据是否同步:
    
    mysql> show databases;
    
    +--------------------+
    
    | Database |
    
    +--------------------+
    
    | information_schema |
    
    | mysql |
    
    | performance_schema |
    
    | student |
    
    | sys |
    
    +--------------------+
    
    5 rows in set (0.00 sec)
    
    
    
    mysql> use student;
    
    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_student |
    
    +-------------------+
    
    | student |
    
    +-------------------+
    
    1 row in set (0.00 sec)
    
    
    
    mysql> select * from student;
    
    +----+------+------+
    
    | id | name | age |
    
    +----+------+------+
    
    | 1 | tom | 20 |
    
    +----+------+------+
    
    1 row in set (0.00 sec)
    
    
  • 相关阅读:
    ansible语法
    hadoop本地模式安装及配置
    linux免密钥登录
    ansible配置文件
    ansible(安装)
    elasticsearch基本概念
    elasticsearch基本概念
    elasticsearch倒排索引介绍
    Flex tree展开节点问题!
    Oracle-Trigger-Insert tableA and tableB
  • 原文地址:https://www.cnblogs.com/liping0826/p/12092398.html
Copyright © 2011-2022 走看看