数据库集群技术

news2024/11/16 3:51:00

源码安装mysql

mysql.com

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

官网下载

在这里插入图片描述

  1. 安装依赖性:
[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. 下载并解压源码包
[root@mysql-node2 ~]# tar zxf mysql-boost-5.7.44.tar.gz 
[root@mysql-node2 ~]# cd /root/mysql-5.7.44
  1. 源码编译安装mysql
[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即可

在这里插入图片描述

image-20240822092011954

[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

有个数据库密码

在这里插入图片描述

[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 开机启动

安全初始化
[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! 
[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)
创建用户
mysql> CREATE USER repl@'%'^CDENTIFIED BY 'MySQL@123';
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
Query OK, 0 rows affected (0.00 sec)
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

从库上

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   #  数据回放
     ...省略...

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

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)

从库

从库应该不能写

第三台数据库

[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要等多久进行回放

在从库上

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语句,就可以开启这个功能,它可以让你很容易地知道哪些语句是需要优化的。
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

测试慢查询

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中设定

[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 #日志回放恢复功能开启
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

主库

[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

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"    

数据库配置

[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

搭建一主两从架构

#在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

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中

[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

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

[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
在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
[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未出现故障手动切换

[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故障手动切换

#模拟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

自动切换

[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

恢复故障节点

[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功能

#上传在群中发给大家的脚本
[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

模拟故障

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

恢复故障主机

[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变化

[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的读写 只是看一下

#安装mysql-router
[root@mysql_node1 ~]# rpm -ivh mysql-router-community-8.4.0-1.el7.x86_64.rpm
[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.

建立用户并赋予权限

mysql> alter user root@localhost identified by 'yu';
mysql> create user lee@'%' identified by 'MySQL@123';
mysql> grant all on *.* to lee@'%';
[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故障手动切换

[root@mysql-node20 mysql]# /etc/init.d/mysqld stop
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2076510.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Netty系列-1 NioEventLoopGroup和NioEventLoop介绍

背景 从本文开始开启一个新的专题Netty系列&#xff0c;用于收集Netty相关的文章&#xff0c;内容包含Netty的使用方式、运行原理等。 基于io.netty:netty-all:4.1.49.Final版本进行介绍 1.NioEventLoopGroup 介绍NioEventLoopGroup之前&#xff0c;有几个相关的组件需要提前…

idea导入maven项目(别人的项目)爆红

作为一个经常学习交流的人&#xff0c;或者工作需要&#xff0c;我们都或多或少会把别人写好的代码拷贝过来学习或编辑&#xff0c;大多数时候都是把整个项目拿过来;但是往往把代码拿到之后放在自己电脑用 idea 打开的时候就会出现 pom.xml 文件红线报错&#xff0c;然后倒入的…

大模型企业应用落地系列》基于大模型的对话式推荐系统》技术架构设计全攻略

注&#xff1a;此文章内容均节选自充电了么创始人&#xff0c;CEO兼CTO陈敬雷老师的新书《自然语言处理原理与实战》&#xff08;人工智能科学与技术丛书&#xff09;【陈敬雷编著】【清华大学出版社】 文章目录 大模型企业应用落地系列全貌基于大模型的对话式推荐系统》技术架…

如何使用ssm实现投稿系统+vue

TOC ssm231论文投稿系统vue 系统概述 1.1 研究背景 如今互联网高速发展&#xff0c;网络遍布全球&#xff0c;通过互联网发布的消息能快而方便的传播到世界每个角落&#xff0c;并且互联网上能传播的信息也很广&#xff0c;比如文字、图片、声音、视频等。从而&#xff0c;…

软件测试 | 概念(1)

目录 前言 需求的概念 开发模型 软件的生命周期 常见开发模型 瀑布模型 螺旋模型 增量模型&#xff0c;迭代模型 敏捷模型 Scrum模型 测试模型 V模型 W模型&#xff08;双V模型&#xff09; 前言 测试&#xff1a;验证软件的特性是否满足用户的需求。 用户的需求…

vue3前端界面布置到服务器,使用户能用网址访问到界面

1.下载Nginx&#xff1a; nginx: download 2.下载好的Nginx解压缩&#xff0c; 在解压缩的文件夹下找到conf > nginx.conf&#xff0c;修改nginx.conf中的server&#xff0c;配置服务器的ip地址和端口号 3.执行npm run build命令&#xff0c;vue生成的dist下的文件全部放置在…

基于微信小程序的行李寄存管理系统的设计与实现(论文+源码)_kaic

基于微信小程序的行李寄存管理系统的设计与实现(论文源码)_kaic 摘 要 人们外出旅行的时候&#xff0c;经常会需要到行李寄存的服务。行李寄存处在全国各地都很常见。现存的行李寄存方式很传统&#xff0c;适合小规模的行李寄存&#xff0c;当行李数量较多时&#xff0c;就…

【领域驱动设计 打通DDD最小闭环】三 模型的建立-领域建模

本篇BLOG为DDD流程的第二步&#xff0c;在模型的建立阶段&#xff0c;领域专家与技术人员通过领域建模来完成更为细致的模型建立讨论 领域建模的目的 领域建模主要有两个目的&#xff1a; 将知识可视化&#xff0c;准确、深刻地反映领域知识&#xff0c;并且在业务和技术人…

神经网络——非线性激活

1 非线性激活 1.1 几种常见的非线性激活&#xff1a; ReLU (Rectified Linear Unit)线性整流函数 Sigmoid 1.2代码实战&#xff1a; 1.2.1 ReLU import torch from torch import nn from torch.nn import ReLUinputtorch.tensor([[1,-0.5],[-1,3]])inputtorch.reshape(…

HT97226 160mW免输出耦合电容的立体声耳机放大器

特点&#xff1a; 输出无需隔直流电容 卓越的低音效果 无咔嗒/噼噗声&#xff0c;50uV (typical) Vos 低THDN:最低0.002% 低噪声,VN: 8.5uV 支持单端输入和全差分输入 2.5V至6V较宽的电源工作范围 输出功率:80mW(fIN1kHz,VDD3.6V,RL32Ω, THDN1%) 160mW(PVDD5V,fIN1kHz,RL32Ω…

Java中的抽象类 abstract

抽象方法&#xff1a; 将共性的行为&#xff08;方法&#xff09;抽取到父类之后。由于每一个子类执行的内容不一样&#xff0c;所以&#xff0c;在父类中不能确定具体的方法体。该方法就可以定义为抽象方法。 抽象类 如果一个类中存在抽象方法&#xff0c;那么该类就必须声…

【软件测试】软件测试-----概念篇

软件测试相关概念 一.需求的相关概念1.1 用户需求1.2 软件需求 二. 开发模型2.1 模型的基本概念.2.2 软件的生命周期2.2.1 理解软件生命周期每个阶段的具体任务 2.3 常见的开发模型.2.3.1 瀑布模型(适用场景&#xff1a;需求固定的小项目).2.3.2 螺旋模型(适用场景&#xff1a;…

ollama+llama3.1 405B 简介

ollamallama3.1 简介 Llama 3.1是一款来自Meta的最新型号&#xff0c;提供8B、70 B和405 B模型。 llama3.1:latestllama3.1:8bllama3.1:70bllama3.1:405bllama3.1:8b-instruct-fp16llama3.1:8b-instruct-q2_Kllama3.1:8b-instruct-q3_K_Sllama3.1:8b-instruct-q3_K_Mllama3.1…

python如何调用另一个文件中的函数

在同一个文件夹下 调用函数&#xff1a; A.py文件&#xff1a; def add(x,y):print(和为&#xff1a;%d%(xy)) B.py文件&#xff1a; import A A.add(1,2) 或 from A import add add(1,2) 在不同文件夹下 A.py文件的文件路径&#xff1a;E:\PythonProject\winycg B.py文件&a…

构建并升级openssh至OpenSSH_9.8p1

组件说明OpenSSH_9.8p1最新版本&#xff08;2024年8月&#xff09;OpenSSL 1.1.1pCentOS7中默认是OpenSSL 1.0.2k-fips 26 Jan 2017版本&#xff0c;OpenSSH_9.8p1不支持CentOS7主要是因为有大量CentOS老版本需要升级RPM&#xff0c;需要适配&#xff0c;故选择此版本。AnolisO…

输入一个正的奇数n(1≤n≤9),打印一个高度为n的、由“*”组成的沙漏图案。当n=5时,输出如下沙漏图案:

输入一个正的奇数n&#xff08;1≤n≤9&#xff09;&#xff0c;打印一个高度为n的、由“*”组成的沙漏图案。当n5时&#xff0c;输出如下沙漏图案&#xff1a; int main(){int i,j,n,m;scanf("%d",&n);m n / 2;for(im1;i<1;i--){ //m1是中间数for(jm1-i;j&g…

音频筑基:为啥一个压缩率概念,中文搜索结果都是错的?

音频筑基&#xff1a;为啥一个压缩率概念&#xff0c;中文搜索结果都是错的&#xff1f; 缘起概念分析小结 缘起 最近看一些数据压缩类的文章&#xff0c;对不同场合下表达的压缩率概念分歧&#xff0c;产生了疑问。有的说&#xff0c;压缩率越小越好&#xff0c;有的又说&…

信刻光盘摆渡机——完全物理隔离,安全合规

信刻光盘摆渡机是一款跨网安全数据摆渡设备&#xff0c;用于不同等级网络之间数据跨网安全传输的需求&#xff0c;采用智能光盘机械手臂&#xff0c;模拟人工取放光盘&#xff0c;在保持物理隔离的条件下&#xff0c;安全合规实现网间信息系统数据库及文件同步、网间信息数据交…

下载B站视频作为PPT素材

下载B站视频作为PPT素材 1. 下载原理2. 网页分析3. 请求页面&#xff0c;找到数据4. 数据解析5. 音频、视频下载6. 合并音频与视频7. 完整代码 其实使用爬虫也不是第一次了&#xff0c;之前从网站爬过图片&#xff0c;下载过大型文件&#xff0c;如今从下载视频开始才想到要写一…

搭建自己的GPT

搭建自己的GPT 文章说明核心代码效果展示源码下载 文章说明 目前GPT的使用比较主流&#xff0c;现有开源大模型&#xff0c;可以拉取到本地进行部署&#xff0c;搭建属于自己的GPT对话工具&#xff1b;主要用于熟悉大模型的本地搭建&#xff1b;本文采用开源的Ollama进行服务提…