数据库集群技术

源码安装mysql

mysql.com

[https://downloads.mysql.com/archives/community/\]:

官网下载

  1. 安装依赖性:
bash 复制代码
[root@mysql-node2 ~]# dnf install cmake gcc-c++ openssl-devel \ ncurses-devel.x86_64 libtirpc-devel-1.3.3-8.el9_4.x86_64.rpm rpcgen.x86_64 
  1. 下载并解压源码包
bash 复制代码
[root@mysql-node2 ~]# tar zxf mysql-boost-5.7.44.tar.gz 
[root@mysql-node2 ~]# cd /root/mysql-5.7.44
  1. 源码编译安装mysql
bash 复制代码
[root@mysql_node2 mysql-5.7.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/

[root@mysql-node2 mysql-5.7.44]# make -j2      #-j2 表示有几个核心就跑几个进程
[root@mysql-node2 mysql-5.7.44# make install

note: 当cmake出错后如果想重新检测,删除 mysql-5.7.44 中 CMakeCache.txt即可

bash 复制代码
[root@mysql_node1 mysql-5.7.44]# useradd -s /sbin/nologin -M mysql
[root@mysql_node1 mysql-5.7.44]# mkdir -p /data/mysql
[root@mysql_node1 mysql-5.7.44]# chown -R mysql:mysql /data/mysql/
[root@mysql_node1 mysql-5.7.44]# ll -d /data/mysql/
drwxr-xr-x 2 mysql mysql 6 Aug 22 10:28 /data/mysql/

[root@mysql_node2 mysql-5.7.44]# cd support-files/
[root@mysql_node2 support-files]# ls
build-tags              CTestTestfile.cmake  mysqld_multi.server     mysql.server
CMakeFiles              dtrace               mysqld_multi.server.sh  mysql.server.sh
cmake_install.cmake     MacOSX               mysql-log-rotate        mysql.server-sys5.sh
CMakeLists.txt          magic                mysql-log-rotate.sh     plugins.files
compiler_warnings.supp  Makefile             mysql.m4
[root@mysql_node2 support-files]# vim mysql.server
[root@mysql_node2 support-files]# cp mysql.server /etc/init.d/mysqld
[root@mysql_node2 support-files]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0

设置环境变量

[root@mysql_node1 support-files]# vim ~/.bash_profile 
[root@mysql_node1 support-files]# source ~/.bash_profile 

数据初始化

mysqld --user=mysql --initialize

有个数据库密码

bash 复制代码
[root@mysql_node1 support-files]# chmod +x  /etc/init.d/mysqld
[root@mysql_node1 support-files]# cd
[root@mysql_node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql_node1.err'.
 SUCCESS! 
 
[root@mysql_node1 ~]# yum install chkconfig -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

rhel7                                                                               | 2.8 kB  00:00:00     
Package chkconfig-1.7.6-1.el7.x86_64 already installed and latest version
Nothing to do
[root@mysql_node1 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rhnsd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

默认2345
[root@mysql_node1 ~]# chkconfig mysqld on
[root@mysql_node1 ~]# chkconfig --list

chkconfig 开机启动

bash 复制代码
安全初始化
[root@mysql_node2 support-files]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
Sorry, passwords do not match.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@mysql_node2 support-files]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 
bash 复制代码
[root@mysql_node1 ~]# vim  /etc/my.cnf
[mysqld]
server-id=0
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin

[root@mysql_node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 

[root@mysql_node1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 Source distribution

Copyright (c) 2000, 2023, 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> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)
bash 复制代码
创建用户
mysql> CREATE USER repl@'%'^CDENTIFIED BY 'MySQL@123';
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
Query OK, 0 rows affected (0.00 sec)
bash 复制代码
mysql> SHOW MASTER STATUS\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

从库上

bash 复制代码
mysql>  CHANGE MASTER TO MASTER_HOST='192.168.136.10',MASTER_USER='repl',MASTER_PASSWORD='MySQL@123',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.136.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_node2-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes   #数据传输
            Slave_SQL_Running: Yes   #  数据回放
     ...省略...

主库上创建主机库和表,插入数据

bash 复制代码
mysql> create database yu;
Query OK, 1 row affected (0.00 sec)
mysql> create table yu.userlist( 
    -> username varchar(10) not null,
    -> password varchar(50) not null);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into yu.userlist values('jie1','123');
Query OK, 1 row affected (0.00 sec)

mysql> select * from yu.userlist;
+----------+----------+
| username | password |
+----------+----------+
| jie1     | 123      |
+----------+----------+
1 row in set (0.00 sec)

从库

从库应该不能写

第三台数据库

bash 复制代码
[root@mysql_node1 ~]# rsync -al /usr/local/mysql root@192.168.136.30:/usr/local

[root@mysql_node3 ~]# useradd -s /sbin/nologin -M mysql
[root@mysql_node3 ~]# mkdir -p /data/mysql
[root@mysql_node3 ~]# chown mysql:mysql /data/mysql/
[root@mysql_node3 ~]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@mysql_node3 ~]# vim /etc/my.cnf
添加
server-id=30
要想从库只读加这行
# super_read_only=on  这里从库还要传数据就先不添加
[root@mysql_node3 ~]# vim ~/.bash_profile 
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
[root@mysql_node3 ~]# source ~/.bash_profile 

[root@mysql_node3 ~]# mysqld --user=mysql --initialize
[root@mysql_node3 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/var/lib/mysql/mysql_node3.err'.
 SUCCESS! 
[root@mysql_node3 ~]# mysql_secure_installation

拉平数据

mysqldump

延迟复制

延迟复制时用来控制sql线程的,和i/o线程无关 这个延迟复制不是i/o线程过段时间来复制,i/o是正常工作的 是日志已经保存在slave端了,那个sql要等多久进行回放

在从库上

bash 复制代码
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_delay=60;
Query OK, 0 rows affected (0.00 sec)

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

mysql> show slave status\G;            
            Master_Server_Id: 10
                  Master_UUID: 8d77eb5c-602f-11ef-aba6-000c29ce05c4
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 60
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400

测试: 在master中写入数据后过了延迟时间才能被查询到

慢查询日志

  • 慢查询,顾名思义,执行很慢的查询
  • 当执行SQL超过long_query_time参数设定的时间阈值(默认10s)时,就被认为是慢查询,这个 SQL语句就是需要优化的
  • 慢查询被记录在慢查询日志里
  • 慢查询日志默认是不开启的
  • 如果需要优化SQL语句,就可以开启这个功能,它可以让你很容易地知道哪些语句是需要优化的。
bash 复制代码
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.00 sec)

mysql> set lone_query_time=4;
ERROR 1193 (HY000): Unknown system variable 'lone_query_time'
mysql> set long_query_time=4;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES like "long%";
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 4.000000 |
+-----------------+----------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES like "slow%";
+---------------------+----------------------------------+
| Variable_name       | Value                            |
+---------------------+----------------------------------+
| slow_launch_time    | 2                                |
| slow_query_log      | ON                               |
| slow_query_log_file | /data/mysql/mysql_node3-slow.log |
+---------------------+----------------------------------+
3 rows in set (0.00 sec)

mysql> \q
Bye

[root@mysql_node3 ~]# cat /data/mysql/mysql_node3-slow.log 
/usr/local/mysql/bin/mysqld, Version: 5.7.44 (Source distribution). started with:
Tcp port: 3306  Unix socket: /data/mysql/mysql.sock
Time                 Id Command    Argument

测试慢查询

bash 复制代码
mysql> select sleep (10);
+------------+
| sleep (10) |
+------------+
|          0 |
+------------+
1 row in set (10.00 sec)

mysql> \q
Bye
            
[root@mysql_node3 ~]# cat /data/mysql/mysql_node3-slow.log
/usr/local/mysql/bin/mysqld, Version: 5.7.44 (Source distribution). started with:
Tcp port: 3306  Unix socket: /data/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 2024-08-22T08:25:37.103021Z
# User@Host: root[root] @ localhost []  Id:    11
# Query_time: 10.000824  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1724315137;
select sleep (10);

mysql的并行复制

查看slave中的线程信息

mysql> show processlist;
+----+------+----------------------+------+-------------+-------+---------------------------------------------------------------+------------------+
| Id | User | Host                 | db   | Command     | Time  | State                                                         | Info             |
+----+------+----------------------+------+-------------+-------+---------------------------------------------------------------+------------------+
|  7 | repl | 192.168.136.20:59800 | NULL | Binlog Dump | 17116 | Master has sent all binlog to slave; waiting for more updates | NULL             |
|  9 | repl | 192.168.136.30:37594 | NULL | Binlog Dump |  4434 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 20 | root | localhost            | NULL | Query       |     0 | starting                                                      | show processlist |
+----+------+----------------------+------+-------------+-------+---------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

mysql的并行复制

默认情况下slave中使用的是sql单线程回放

在master中时多用户读写,如果使用sql单线程回放那么会造成组从延迟严重 开启MySQL的多线程回放可以解决上述问题

在slaves中设定

bash 复制代码
[root@mysql-node2 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=10
gtid_mode=ON
enforce-gtid-consistency=ON
slave-parallel-type=LOGICAL_CLOCK #基于组提交,
slave-parallel-workers=16 #开启线程数量
master_info_repository=TABLE #master信息在表中记录,默认记录
在/data/mysql/master.info
relay_log_info_repository=TABLE #回放日志信息在表中记录,默认记录
在/data/mysql/relay-log.info
relay_log_recovery=ON #日志回放恢复功能开启
bash 复制代码
mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  2 | system user |           | NULL | Connect |   83 | Slave has read all relay log; waiting for more updates | NULL             |
|  3 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  4 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  5 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  6 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  7 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  8 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
|  9 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 10 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 12 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 13 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 14 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 15 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 16 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 17 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 18 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 19 | system user |           | NULL | Connect |   83 | Waiting for an event from Coordinator                  | NULL             |
| 20 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
18 rows in set (0.00 sec)

此时sql线程转化为协调线程,16个worker负责处理sql协调线程发送过来的处理请求

Note

MySQL 组提交(Group commit)是一个性能优化特性,它允许在一个事务日志同步操作中将多个 事务的日志记录一起写入。这样做可以减少磁盘I/O的次数,从而提高数据库的整体性能。

导入数据

mysql -uroot -pAB12qq,, -e "CREATE DATABASE yu;"
mysql -uroot -pAB12qq,, yu < yu.sql

mysql-MGR

配置解析文件

vim /etc/hosts
192.168.136.10 mysql_node1
192.168.136.20 mysql_node2
192.168.136.30 mysql_node3

主库

bash 复制代码
[root@mysql_node1 ~]# vim /etc/my.cnf
[mysqld]
server-id=10
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="192.168.136.10:33061"
group_replication_group_seeds="192.168.136.10:33061,192.168.136.20:33061,192.168.136.30:33061"
group_replication_ip_whitelist="192.168.136.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1

配置sql

python 复制代码
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'lee';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>  CREATE USER rpl_user@'%' IDENTIFIED BY 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='MySQL@123' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>  SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
^HQuery OK, 0 rows affected, 1 warning (2.24 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;
配置从库

配置文件只要改一行 20 30主机都一样

group_replication_local_address="192.168.136.20:33061"    

数据库配置

bash 复制代码
[root@mysql_node2 ~]# mysql -uroot -pMySQL@123
mysql>  SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'MySQL@123';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='MySQL@123' FOR CHANNEL
    -> 'group_replication_recovery';
mysql> START GROUP_REPLICATION;

mha

搭建一主两从架构

bash 复制代码
#在master节点中
[root@mysql_node1 ~]# /etc/init.d/mysqld stop
[root@mysql_node1 ~]# vim /etc/my.cnf
[mysqld]
server-id=10
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin
gtid-mode=on
log_slave_updates=on
enforce-gtid-consistency=on

[root@mysql_node1 ~]# /etc/init.d/mysqld start
[root@mysql_node1 ~]# mysql -uroot -p
mysql> create user rep@'%' identified by 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to rep@'%';
Query OK, 0 rows affected (0.00 sec)

#安装半同步插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
#开启半同步
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

从库

20 30主机上

在/etc/my.cnf 将server-id 更改

进入mysql

bash 复制代码
mysql> CHANGE MASTER TO MASTER_HOST='192.168.136.10', MASTER_USER='rep', MASTER_PASSWORD='MySQL@123', MASTER_AUTO_POSITION=1;
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.136.10
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 837
               Relay_Log_File: mysql_node2-relay-bin.000003
                Relay_Log_Pos: 1050
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

安装MHA所需要的软件

在MHA中

bash 复制代码
[root@mha ~]# unzip MHA-7.zip 
[root@mha ~]# cd MHA-7/
[root@mha MHA-7]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm  perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
mha4mysql-manager-0.58.tar.gz                   perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm     perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm          perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm  perl-Net-Telnet-3.03-19.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm       perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@mha MHA-7]# yum install *.rpm

将一些安装包传送给三台数据库主机

bash 复制代码
[root@mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@192.168.136.10:/mnt
[root@mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@192.168.136.20:/mnt
[root@mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@192.168.136.30:/mnt
bash 复制代码
在sql-node中
[root@mysql_node1 /mnt]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
[root@mysql_node2 /mnt]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
[root@mysql_node3 /mnt]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
bash 复制代码
[root@mysql-mha ~]# mkdir /etc/masterha
[root@mysql-mha MHA-7]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@mysql-mha MHA-7]# cd mha4mysql-manager-0.58/samples/conf/
[root@mha mha4mysql-manager-0.58]# cd samples/conf
[root@mha conf]# cat masterha_default.cnf app1.cnf > /etc/masterha/app1.cnf 
[root@mha conf]# vim /etc/masterha/app1.cnf 

[server default]
user=root
password=MySQL@123
ssh_user=root
repl_user=rep
repl_password=MySQL@123

master_binlog_dir= /data/mysql
remote_workdir=/tmp
secondary_check_script= masterha_secondary_check -s 192.168.136.10 -s 192.168.136.11
ping_interval=3
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
[server default]
manager_workdir=/etc/masterha
manager_log=/etc/masterha/manager.log

[server1]
hostname=192.168.136.10
candidate_master=1
check_repl_delay=0

[server2]
hostname=192.168.136.20
candidate_master=1
check_repl_delay=0

[server3]
hostname=host3
no_master=1

主机之间配置免密登陆

ssh-keygen

ssh-copy-id root@xxx.xxx.xxx.xxx

检测网络及ssh免密

检测数据主从复制情况

#在数据节点master端
mysql> grant all on *.* to root@'%' identified by 'MySQL@123';   #允许root远程登陆
Query OK, 0 rows affected, 1 warning (0.00 sec)

切换方式:

master未出现故障手动切换

bash 复制代码
[root@mha ~]# masterha_master_switch \
> --conf=/etc/masterha/app1.cnf \
> --master_state=alive \
> --new_master_host=192.168.136.20 \
> --new_master_port=3306 \
> --orig_master_is_new_slave \
> --running_updates_limit=10000

...省略...
It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.136.10(192.168.136.10:3306)? (YES/no): yes
Sat Aug 24 20:32:37 2024 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
...省略...
Sat Aug 24 20:32:37 2024 - [info] 
From:
192.168.136.10(192.168.136.10:3306) (current master)
 +--192.168.136.20(192.168.136.20:3306)
 +--192.168.136.30(192.168.136.30:3306)

To:
192.168.136.20(192.168.136.20:3306) (new master)
 +--192.168.136.30(192.168.136.30:3306)
 +--192.168.136.10(192.168.136.10:3306)

Starting master switch from 192.168.136.10(192.168.136.10:3306) to 192.168.136.20(192.168.136.20:3306)? (yes/NO): yes
Sat Aug 24 20:32:42 2024 - [info] Checking whether 192.168.136.20(192.168.136.20:3306) is ok for the new master..
...省略...
Sat Aug 24 20:32:42 2024 - [info] 
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): yes
Sat Aug 24 20:33:33 2024 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
...省略...
Sat Aug 24 20:33:36 2024 - [info]  192.168.136.20: Resetting slave info succeeded.
Sat Aug 24 20:33:36 2024 - [info] Switching master to 192.168.136.20(192.168.136.20:3306) completed successfully.

检测:

在mysql_node1之前的主机上查看

master故障手动切换

bash 复制代码
#模拟master故障
[root@mysql-node2 mysql]# /etc/init.d/mysqld stop
#在MHA-master中做故障切换
[root@mysql-mha masterha]# masterha_master_switch --master_state=dead --
conf=/etc/masterha/app1.cnf --dead_master_host=192.168.136.20 --
dead_master_port=3306 --new_master_host=192.168.136.30 --new_master_port=3306 --
ignore_last_failover
注意:--ignore_last_failover 表示忽略在/etc/masterha/目录中在切换过程中生成的锁文件

[root@mysql-node2 tmp]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql-node2 tmp]# mysql -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.136.10', MASTER_USER='rep',
MASTER_PASSWORD='MySQL@123', MASTER_AUTO_POSITION=1;
mysql> show slave\G
#测试一主两从是否正常
[root@mysql-mha masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf

自动切换

bash 复制代码
[root@mysql-mha masterha]# rm -fr app1.failover.complete #删掉切换锁文件

#监控程序通过指定配置文件监控master状态,当master出问题后自动切换并退出避免重复做故障切换
[root@mysql-mha masterha]# masterha_manager --conf=/etc/masterha/app1.cnf
[root@mysql-mha masterha]# cat /etc/masterha/manager.log

恢复故障节点

bash 复制代码
[root@mysql-node2 mysql]# /etc/init.d/mysqld start
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='rep',
MASTER_PASSWORD='MySQL@123', MASTER_AUTO_POSITION=1

清除锁文件

[root@mysql-mha masterha]# rm -rf app1.failover.complete manager.log

为MHA添加VIP功能

bash 复制代码
#上传在群中发给大家的脚本
[root@mysql-mha ~]# ls
master_ip_failover master_ip_online_change MHA-7 MHA-7.zip
[root@mysql-mha ~]# cp master_ip_failover master_ip_online_change /usr/local/bin/
[root@mysql-mha ~]# chmod +x /usr/local/bin/master_ip_*
#修改脚本在脚本中只需要修改下vip即可
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_failover
my $vip = '172.25.254.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_online_change
my $vip = '172.25.254.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
my $exit_code = 0;
[root@mysql-mha masterha]# masterha_manager --conf=/etc/masterha/app1.cnf & 启动监控程序
[root@mysql-node10 tmp]# ip a a 192.168.136.200/24 dev eth0 #在master节点添加VIP

模拟故障

bash 复制代码
[root@mysql-node1 ~]# /etc/init.d/mysqld stop #关闭主节点服务
[root@mysql-mha masterha]# cat manager.log

恢复故障主机

bash 复制代码
[root@mysql-node2 mysql]# /etc/init.d/mysqld start
mysql> CHANGE MASTER TO MASTER_HOST='192.168.136.10', MASTER_USER='rep',
MASTER_PASSWORD='MySQL@123', MASTER_AUTO_POSITION=1
[root@mysql-mha masterha]# rm -rf app1.failover.complete manager.log

手动切换后查看vip变化

bash 复制代码
[root@mysql-mha masterha]# masterha_master_switch --conf=/etc/masterha/app1.cnf
--master_state=alive --new_master_host=192.168.136.10 --new_master_port=3306 --
orig_master_is_new_slave --running_updates_limit=10000
[root@mysql-node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
group default qlen 1000
link/ether 00:0c:29:cb:63:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.136.200/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 172.25.254.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever

mysql-router

不能限制mydsql的读写 只是看一下

bash 复制代码
#安装mysql-router
[root@mysql_node1 ~]# rpm -ivh mysql-router-community-8.4.0-1.el7.x86_64.rpm
bash 复制代码
[root@mysql_node1 ~]# vim /etc/mysqlrouter/mysqlrouter.conf
[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 192.168.136.20:3306,192.168.136.30:3306
routing_strategy = round-robin
[root@mysql_node1 ~]# systemctl start mysqlrouter.service
[root@mysql_node1 ~]# netstat -antlupe | grep 7001
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      988        41290      2905/mysqlrouter 

注意:自己不要开mysql

从机

[root@mysql_node2 ~]# mysqld --user=mysql --initialize

-CErT>5e&dq.

建立用户并赋予权限

bash 复制代码
mysql> alter user root@localhost identified by 'yu';
mysql> create user lee@'%' identified by 'MySQL@123';
mysql> grant all on *.* to lee@'%';
bash 复制代码
[root@mysql_node1 ~]# mysql -ulee -pMySQL@123 -h192.168.136.10 -P 7001
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 7
Server version: 5.7.44 Source distribution

Copyright (c) 2000, 2023, 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> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          30 |
+-------------+
1 row in set (0.00 sec)

mysql> \q
Bye
[root@mysql_node1 ~]# mysql -ulee -pMySQL@123 -h192.168.136.10 -P 7001
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 7
Server version: 5.7.44 Source distribution

Copyright (c) 2000, 2023, 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> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
1 row in set (0.00 sec)

master故障手动切换

bash 复制代码
[root@mysql-node20 mysql]# /etc/init.d/mysqld stop
bash 复制代码
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.136.20 --dead_master_port=3306 --new_master_host=192.168.168.10 --new_master_port=3306 --ignore_last_failover
相关推荐
喵叔哟7 分钟前
重构代码之移动字段
java·数据库·重构
念白44310 分钟前
智能病历xml提取
数据库·sql·oracle
qingy_204614 分钟前
【JavaWeb】JavaWeb入门之XML详解
数据库·oracle
大数据面试宝典18 分钟前
用AI来写SQL:让ChatGPT成为你的数据库助手
数据库·人工智能·chatgpt
努力的小雨23 分钟前
快速上手 KSQL:轻松与数据库交互的利器
数据库·经验分享
Gentle58625 分钟前
labview中连接sql server数据库查询语句
数据库·labview
Gentle58627 分钟前
labview用sql server数据库存取数据到一个单元格
数据库·labview
2401_8576363929 分钟前
共享汽车管理新纪元:SpringBoot框架应用
数据库·spring boot·汽车
菲兹园长30 分钟前
表的设计(MYSQL)
数据库·mysql
Java Fans1 小时前
MySQL数据库常用命令大全(完整版——表格形式)
数据库·mysql