Ubuntu22.04 搭建 MySQL8.0.45 MGR
一、环境准备
| 主机名 | IP | MySQL版本 | 系统环境 |
|---|---|---|---|
| root@node0 | 192.168.143.128 | 8.0.45 | Ubuntu22.04 |
| root@node1 | 192.168.143.129 | 8.0.45 | Ubuntu22.04 |
| root@node2 | 192.168.143.130 | 8.0.45 | Ubuntu22.04 |
三台4G2核虚拟机,搭建Ubuntu22.04系统。
1. 设置主机名
-
node0(192.168.143.128):
bashhostnamectl set-hostname node0 -
node1(192.168.143.129):
bashhostnamectl set-hostname node1 -
node2(192.168.143.130):
bashhostnamectl set-hostname node2
2. 配置hosts
三台节点均执行:
bash
vi /etc/hosts
在文件末尾添加:
192.168.143.128 node0
192.168.143.129 node1
192.168.143.130 node2
3. 放置软件包并解压
三台节点均执行:
bash
mkdir /usr/src/mysql && cd /usr/src/mysql
下载软件包(二选一)
-
方式1:服务器wget下载
bashwget -c --load-cookies=cookies.txt "http://seafile.szzt.com.cn/f/2003ef096e584044b8cb/?dl=1" -O "mysql-server_8.0.45-1ubuntu22.04_amd64.deb-bundle.tar" -
方式2:浏览器下载后上传至三台服务器
解压软件包
bash
tar -xvf mysql-server_8.0.45-1ubuntu22.04_amd64.deb-bundle.tar
解压后生成文件:
- libmysqlclient21_8.0.45-1ubuntu22.04_amd64.deb
- libmysqlclient-dev_8.0.45-1ubuntu22.04_amd64.deb
- mysql-client_8.0.45-1ubuntu22.04_amd64.deb
- mysql-common_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-client_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-client-core_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-client-plugins_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-server_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-server-debug_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-test_8.0.45-1ubuntu22.04_amd64.deb
- mysql-community-test-debug_8.0.45-1ubuntu22.04_amd64.deb
- mysql-server_8.0.45-1ubuntu22.04_amd64.deb
- mysql-testsuite_8.0.45-1ubuntu22.04_amd64.deb
二、安装软件包
三台节点均执行以下步骤:
1. 安装基础依赖(按顺序执行)
bash
dpkg -i mysql-common_8.0.45-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-client-plugins_8.0.45-1ubuntu22.04_amd64.deb
2. 安装客户端库
bash
dpkg -i libmysqlclient21_8.0.45-1ubuntu22.04_amd64.deb
dpkg -i libmysqlclient-dev_8.0.45-1ubuntu22.04_amd64.deb
3. 安装客户端核心组件
bash
dpkg -i mysql-community-client-core_8.0.45-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-client_8.0.45-1ubuntu22.04_amd64.deb
dpkg -i mysql-client_8.0.45-1ubuntu22.04_amd64.deb
4. 安装服务端核心组件
bash
dpkg -i mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb
报错信息
bash
root@node0:/usr/src/mysql# dpkg -i mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb
(Reading database ... 101965 files and directories currently installed.)
Preparing to unpack mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb ...
Unpacking mysql-community-server-core (8.0.45-1ubuntu22.04) over (8.0.45-1ubuntu22.04) ...
dpkg: dependency problems prevent configuration of mysql-community-server-core:
mysql-community-server-core depends on libmecab2 (>= 0.996); however:
Package libmecab2 is not installed.
mysql-community-server-core depends on libnuma1 (>= 2.0.11); however:
Package libnuma1 is not installed.
dpkg: error processing package mysql-community-server-core (--install):
dependency problems - leaving unconfigured
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
Errors were encountered while processing:
mysql-community-server-core
5. 报错解决
bash
apt install -y libmecab2 libnuma1
# 重新执行安装
dpkg -i mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb
执行结果
bash
root@node0:/usr/src/mysql# dpkg -i mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb
(Reading database ... 101976 files and directories currently installed.)
Preparing to unpack mysql-community-server-core_8.0.45-1ubuntu22.04_amd64.deb ...
Unpacking mysql-community-server-core (8.0.45-1ubuntu22.04) over (8.0.45-1ubuntu22.04) ...
Setting up mysql-community-server-core (8.0.45-1ubuntu22.04) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
6. 继续安装
bash
apt install -y psmisc --fix-broken
apt -f install ./mysql-community-server_8.0.45-1ubuntu22.04_amd64.deb
执行结果
bash
root@node0:/usr/src/mysql# apt install -y psmisc --fix-broken
root@node0:/usr/src/mysql# apt -f install ./mysql-community-server_8.0.45-1ubuntu22.04_amd64.deb
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
psmisc
0 upgraded, 1 newly installed, 0 to remove and 60 not upgraded.
2 not fully installed or removed.
Need to get 119 kB of archives.
After this operation, 463 kB of additional disk space will be used.
Get:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 psmisc amd64 23.4-2build3 [119 kB]
Fetched 119 kB in 2s (51.7 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package psmisc.
(Reading database ... 102001 files and directories currently installed.)
Preparing to unpack .../psmisc_23.4-2build3_amd64.deb ...
Unpacking psmisc (23.4-2build3) ...
Setting up psmisc (23.4-2build3) ...
Setting up mysql-community-server (8.0.45-1ubuntu22.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
Configuring mysql-community-server
Please provide a strong password that will be set for the root
account of your MySQL database. Leave it blank to enable password
less login using UNIX socket based authentication.
Enter root password: Szzt@123
Now that you have selected a password for the root account,
please confirm by typing it again. Do not share the password with
anyone.
Re-enter root password: Szzt@123
MySQL 8 uses a new authentication based on improved SHA256-based
password methods. It is recommended that all new MySQL Server
installations use this method going forward. This new
authentication plugin requires new versions of connectors and
clients, with support for this new authentication method
(caching_sha2_password). Currently MySQL 8 Connectors and
community drivers built with libmysqlclient21 support this new
method. Clients built with older versions of libmysqlclient may
not be able to connect to the new server.
To retain compatibility with older client software, the default
authentication plugin can be set to the legacy value
(mysql_native_password) This should only be done if required
third-party software has not been updated to work with the new
authentication method. The change will be written to the file
/etc/mysql/mysql.conf.d/default-auth-override.cnf
After installation, the default can be changed by setting the
default_authentication_plugin server setting.
1. Use Strong Password Encryption (RECOMMENDED)
2. Use Legacy Authentication Method (Retain MySQL 5.x Compatibility)
Select default authentication plugin 1
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up mysql-server (8.0.45-1ubuntu22.04) ...
needrestart is being skipped since dpkg has failed
验证端口监听
bash
:/usr/src/mysql# ss -atnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
三、数据库配置
1. 关闭MySQL服务
三台节点均执行:
bash
service mysql stop
2. 修改配置文件
三台节点均执行:
bash
vim /etc/mysql/mysql.conf.d/mysqld.cnf
在文件末尾添加以下配置(注意:server_id、loose-group_replication_local_address 需按节点修改,loose-group_replication_group_seeds 包含所有节点IP):
ini
# ========================
# 业务数据库必要配置
character-set-server=utf8
log_bin_trust_function_creators=1
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
transaction_isolation = READ-COMMITTED
group_concat_max_len = 102400
# ========================
# ========================
# MGR 集群核心配置
# ========================
# 服务器编号,这个要唯一,建议用IP尾数
server_id = 128
# 开启binlog的GTID模式
gtid_mode = ON
# 开启后MySQL只允许能够保障事务安全,并且能够被日志记录的SQL语句被执行
enforce_gtid_consistency = ON
# 关闭binlog校验
binlog_checksum = NONE
# 确保从库执行的中继日志(relay log)操作被记录到自身的二进制日志(binlog)中
log_slave_updates = ON
# 设置binlog记录模式为"行级模式",即记录数据行的变更细节(而非SQL语句)
binlog_format = ROW
# 定义用于事务期间哈希写入提取的算法,组复制模式下必须设置为XXHASH64
transaction_write_set_extraction = XXHASH64
# MGR专用配置
loose-group_replication_group_name = "161ec121-861b-4f61-b6ed-c969087cc1be"
# 所有节点保持一致,可通过uuidgen命令生成
loose-group_replication_start_on_boot = OFF
# 在组复制恢复阶段,自动从主节点获取公钥,用于加密连接认证
loose-group_replication_recovery_get_public_key = ON
# 确定组复制恢复时是否应用SSL,通常设置为"开",默认"关"
loose-group_replication_recovery_use_ssl = ON
# 当前节点用于组内通信的地址和端口,需确保端口未被占用
loose-group_replication_local_address = "192.168.143.128:33061"
# 集群"种子节点"列表,新节点加入时通过此列表发现集群成员
loose-group_replication_group_seeds = "192.168.143.128:33061,192.168.143.129:33061,192.168.143.130:33061"
loose-group_replication_bootstrap_group = OFF
# ========================
3. 重新初始化数据目录(因大小写参数)
三台节点均执行:
bash
rm -rf /var/lib/mysql
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
mysqld --initialize-insecure --user mysql --datadir=/var/lib/mysql --lower-case-table-names=1
4. 启动服务并配置root密码
启动服务
三台节点均执行:
bash
service mysql start
验证端口
bash
:/usr/src/mysql# ss -atnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 151 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 [::1]:6010 [::]:*
配置root密码(本地+远程)
三台节点均执行:
bash
mysql -uroot -p
进入MySQL命令行后执行:
sql
use mysql;
-- 修改本地root密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Szzt@123';
FLUSH PRIVILEGES;
-- 创建远程root用户
CREATE USER 'root'@'%' IDENTIFIED BY 'Szzt@123';
-- 赋予远程root所有权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
-- 验证用户
select host,user from user;
验证结果
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
5 rows in set (0.00 sec)
四、数据库MGR模式配置
1. 创建同步用户及安装插件
三台节点均执行:
bash
mysql -uroot -p
进入MySQL命令行后执行:
sql
-- 创建主从同步用户
CREATE USER 'rpl_user'@'%' IDENTIFIED BY '@Password1122!';
-- 赋予主从同步权限
GRANT REPLICATION SLAVE ON *.* TO 'rpl_user'@'%';
-- 创建远程管理用户
create user 'warton'@'%' identified by '@Password1122!';
-- 赋予远程用户所有权限
grant all privileges on *.* to 'warton'@'%';
-- 刷新权限
FLUSH PRIVILEGES;
-- 清空Binlog(避免高权限操作导致同步失败)
RESET MASTER;
-- 安装MGR插件
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
操作说明
rpl_user:用于主从数据同步的专用账户;warton:用于远程连接(Navicat/JDBC)的管理账户,避免直接使用root;RESET MASTER:删除初始化阶段产生的Binlog(包含创建用户等高权限操作,rpl_user无权限执行,会导致同步卡死);INSTALL PLUGIN:安装Group Replication核心插件。
2. 主节点(node0/192.168.143.128)初始化集群
sql
-- 开启集群引导模式
SET GLOBAL group_replication_bootstrap_group=ON;
-- 启动MGR集群
START GROUP_REPLICATION;
-- 关闭集群引导模式
SET GLOBAL group_replication_bootstrap_group=OFF;
3. 从节点(node1/192.168.143.129、node2/192.168.143.130)加入集群
node1执行
sql
-- 配置同步账户(用于集群恢复)
CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="@Password1122!" FOR CHANNEL 'group_replication_recovery';
-- 启动MGR
start group_replication;
node2执行
sql
-- 配置同步账户(用于集群恢复)
CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="@Password1122!" FOR CHANNEL 'group_replication_recovery';
-- 启动MGR
start group_replication;
五、验证MGR集群状态
在主节点(node0)执行:
sql
select * from performance_schema.replication_group_members;
验证结果
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | c6b21e47-1d14-11f1-a530-000c2961f0eb | node1 | 3306 | ONLINE | SECONDARY | 8.0.45 | XCom |
| group_replication_applier | c6c9a67e-1d14-11f1-9a3d-000c292c77bb | node0 | 3306 | ONLINE | PRIMARY | 8.0.45 | XCom |
| group_replication_applier | c6f301b7-1d14-11f1-a375-000c294c6c71 | node2 | 3306 | ONLINE | SECONDARY | 8.0.45 | XCom |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.00 sec)