MySQL主从

MySQL主从

主从介绍

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

想几个问题:

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

主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务( 主从作用的备份:备份数据库管理系统,而不是备份数据库)

主从形式

img

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

主从复制原理

img

主从复制步骤:

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

主从复制配置

主从复制配置步骤:

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

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

环境说明:

数据库角色IP应用与系统版本有无数据
主数据库192.168.220.8RHEL8/ mysql-5.7无数据
从数据库192.168.220.9RHEL8/ mysql-5.7无数据
二进制安装MySQL

下载到/usr/src/

[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
debug  kernels  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

解压到/usr/local

[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.34-linux-glibc2.12-x86_64  sbin  share  src

创建用户和组

[root@localhost ~]# groupadd -r mysql
useradd -M -s /sbin/nologin -g mysql mysql

修改属主属组

[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
.....
drwxr-xr-x. 9 mysql mysql 129 Aug 30 04:53 mysql

添加环境变量

[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

建立数据存放目录

[root@localhost ~]# mkdir /opt/data -p
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 30 05:00 data

初始化数据库

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data
2021-08-30T09:04:19.992958Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-30T09:04:20.122553Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-30T09:04:20.144643Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-30T09:04:20.148207Z 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: 42fd1efa-0971-11ec-b90a-000c29bb4cb2.
2021-08-30T09:04:20.149165Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-30T09:04:21.004032Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-30T09:04:21.339873Z 1 [Note] A temporary password is generated for root@localhost: =*tgrrheB1g>
[root@localhost ~]# echo '*tgrrheB1g>' > pass
[root@localhost ~]# ls pass 
pass

查看是否缺少依赖包

[root@localhost ~]# ldd /usr/local/mysql/bin/mysql
(ldd是看某一个程序文件它所依赖的包,如果没有,就不能用,就需要用yum安装,查找哪个包提供的(yum whatprovides pkgs_name))
        linux-vdso.so.1 (0x00007ffe9fd3c000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f9d26825000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f9d2661c000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f9d26418000)
        libncurses.so.5 => not found(缺少两个一样的,所有只需要安装一次)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f9d26083000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f9d25d01000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f9d25ae9000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f9d25727000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9d26a45000)
        libtinfo.so.5 => not found
        
[root@localhost ~]# yum whatprovides libtinfo.so.5
Failed to set locale, defaulting to C.UTF-8
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 6 days, 20:05:31 ago on Mon Aug 23 09:02:10 2021.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo        : baseos
Matched from:
Provide    : libtinfo.so.5

[root@localhost ~]# yum -y install ncurses-compat-libs

生成配置文件

[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve

修改配置文件

[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# vim mysql.server
 44 # overwritten by settings in the MySQL configuration files.
 45 
 46 basedir=/usr/local/mysql   # 安装mysql的位置
 47 datadir=/opt/data          # 数据存放的位置

启动服务

[root@localhost ~]# /usr/local/mysql/support-files/mysql.server 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                          0.0.0.0:22                        0.0.0.0:*            
LISTEN         0              80                                 *:3306                            *:*            
LISTEN         0              128                             [::]:22                           [::]:*

登录数据库修改密码

[root@localhost ~]# mysql -uroot -p'*tgrrheB1g>'
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.34

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> set password=password('redhat');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye
[root@localhost ~]# mysql -uroot -predhat
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 3
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> exit
Bye

从数据库也是同样安装

MySQL主从配置(数据一致时)

查看两边数据库数据是否一致

主库

[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@localhost ~]# mysql -uroot -predhat -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 ~]#  hostnamectl set-hostname salve
[root@localhost ~]# bash
[root@salve ~]# mysql -uroot -predhat -e'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

主从库数据都是一致的

在主数据库里创建一个同步账户授权给从数据库使用
mysql> grant replication(复制) slave on *.* to 'rhel'@'192.168.220.9' identified by 'rhel';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;(刷新)
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@master ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve

# replacation slave config
log-bin = mysql_bin      //启用binlog日志
server-id = 10    //数据库服务器唯一标识符,主库的server-id值必须比从库的小

//重启服务
[root@master ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.Logging to '/opt/data/master.err'.
 SUCCESS!
 
 //查看主库的状态
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)
配置从数据库
[root@salve ~]# cat  /etc/my.cnf
[mysqld]
datadir = /opt/data
basedir = /usr/local/mysql
pid-file = /opt/data/mysql.pid
socket = /tmp/mysql.sock
port = 3306
skip-name-resolve

# replacation slave config
relay-log = mysql_relay   //启用中继日志relay-log   
server-id = 20   //设置从库的唯一标识符,从库的server-id值必须大于主库的值

//重启数据库
[root@salve ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.Logging to '/opt/data/salve.err'.
 SUCCESS!
 
 //配置并启动主从复制
mysql> change master to
    -> master_host='192.168.220.8',  (IP是主库的IP)
    -> master_user='rhel',           (用户是主库创建的同步账户)
    -> master_password='rhel',       (密码是主库创建的同步账户密码)
    -> master_log_file='mysql_bin.000001',  (log_file是主库状态里的文件)
    -> master_log_pos=154;                  (log_pos是主库状态里的Position)
Query OK, 0 rows affected, 2 warnings (0.00 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.220.8
                  Master_User: rhel
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay.000004
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes  //必须是yes,如果有一处不是,尝试关闭主从两边的防火墙和seLinux,重启mysql服务
            Slave_SQL_Running: Yes  //必须是yes

验证

在主库创建库,从库查看

//主库创建school库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

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

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

//从库查看是否同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

MySQL主从配置(数据不一致时)

主从数据库数据不一致时,将主库的数据备份传到从库,再进行配置

查看主库

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

mysql> use school
Database changed
mysql> select * from student;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | zs   |   21 |
+----+------+------+
1 row 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@master ~]# mysqldump -uroot -predhat --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
all.sql  anaconda-ks.cfg

//传输到从库
[root@master ~]# scp /root/all.sql root@192.168.220.9:/root/
The authenticity of host '192.168.220.9 (192.168.220.9)' can't be established.
ECDSA key fingerprint is SHA256:tMfq4r/I6NXr8WxEv9mh+SJI75Dkju8az0Rx6NMuBBY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.9' (ECDSA) to the list of known hosts.
root@192.168.220.9's password: 
Permission denied, please try again.
root@192.168.220.9's password: 
all.sql                                                                         100%  853KB  75.5MB/s   00:00  

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

在从库上恢复主库的文件达到数据一致

[root@salve ~]# ls
all.sql  anaconda-ks.cfg

[root@salve ~]# mysql -uroot -predhat < all.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

[root@salve ~]# mysql -uroot -predhat -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
[root@salve ~]# mysql -uroot -predhat -e 'select * from school.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | zs   |   21 |
+----+------+------+

在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication(复制) slave on *.* to 'rhel'@'192.168.220.9' identified by 'rhel';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;(刷新)
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@master ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve

# replacation slave config
log-bin = mysql_bin      //启用binlog日志
server-id = 10    //数据库服务器唯一标识符,主库的server-id值必须比从库的小

//重启服务
[root@master ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.Logging to '/opt/data/master.err'.
 SUCCESS!
 
 //查看主库的状态
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)
配置从数据库
[root@salve ~]# cat  /etc/my.cnf
[mysqld]
datadir = /opt/data
basedir = /usr/local/mysql
pid-file = /opt/data/mysql.pid
socket = /tmp/mysql.sock
port = 3306
skip-name-resolve

# replacation slave config
relay-log = mysql_relay   //启用中继日志relay-log   
server-id = 20   //设置从库的唯一标识符,从库的server-id值必须大于主库的值

//重启数据库
[root@salve ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.Logging to '/opt/data/salve.err'.
 SUCCESS!
 
 //配置并启动主从复制
mysql> change master to
    -> master_host='192.168.220.8',   (IP是主库的IP)
    -> master_user='rhel',            (用户是主库创建的同步账户)        
    -> master_password='rhel',         (密码是主库创建的同步账户密码)
    -> master_log_file='mysql_bin.000001',   (log_file是主库状态里的文件)
    -> master_log_pos=154;                    (log_pos是主库状态里的Position)
Query OK, 0 rows affected, 2 warnings (0.00 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.220.8
                  Master_User: rhel
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay.000004
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes  //必须是yes,如果有一处不是,尝试关闭主从两边的防火墙和seLinux,重启mysql服务
            Slave_SQL_Running: Yes  //必须是yes
验证

在主库创建库,从库查看

//主库创建info库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

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

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

//从库查看是否同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| info               |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从监控工具是一种用于监控MySQL数据库主从同步状态的工具。它可以帮助管理员实时了解主从同步的情况,及时发现并解决问题,确保数据库的高可用性和数据一致性。 MySQL主从监控工具通常具有以下功能: 1. 数据同步监控:通过监控主从的二进制日志文件和复制线程状态,实时监测主从同步的延迟和错误,如IO线程停止、SQL线程停止等,对备库的同步状态进行实时监控。 2. 健康度监控:通过监控数据库运行状态,如CPU利用率、内存使用情况、硬盘空间等,及时发现并解决可能影响主从同步的健康问题,如资源不足、负载过高等。 3. 数据一致性监控:通过对比主库和备库的数据差异,检测数据一致性问题,如数据丢失、数据不一致等,保证备库数据的准确性。 4. 告警通知:在主从同步发生问题时,通过邮件、短信等方式发送告警通知,及时提醒管理员并采取相应措施,防止问题进一步扩大。 5. 历史数据分析:对主从同步状态进行历史数据的记录和分析,生成报表和图表,以便管理员了解主从同步的趋势和性能变化,为性能优化和故障排查提供支持。 常见的MySQL主从监控工具包括Percona Toolkit、MHA、MySQL Replication Monitor等。这些工具提供了图形化界面和命令行界面,方便管理员进行配置、监控和管理。 总之,MySQL主从监控工具是数据库管理员保证MySQL主从同步高可用性和数据一致性的重要辅助工具,它能够及时发现并解决问题,提高数据库的可靠性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值