成功最有效的方法就是向有经验的人学习!

基于MYSQL5.7.41部署MGR集群(标准作业手册)

说明

本次我们使用虚拟机的方式进行部署安装
之前我们在K8S中部署了一版,实际这一版没啥区别只是翻译了一下。

环境准备

主机名 IP 角色
mysql-mgr-1 10.0.0.130 主节点、引导服务器
mysql-mgr-1 10.0.0.131 主节点
mysql-mgr-2 10.0.0.132 主节点

MYSQL安装

安装准备

准备软件包mysql-5.7.41-linux-glibc2.12-x86_64.tar
我这里统一放在/opt/目录下,下面就是我们所需要的文件

[root@mysql-mgr-1 opt]# ls mysql-5.7.41-linux-glibc2.12-x86_64 -lh
total 268K
drwxr-xr-x  2 root root  4.0K Jul  4 11:00 bin
drwxr-xr-x  2 root root    73 Jul  4 11:00 docs
drwxr-xr-x  3 root root  4.0K Jul  4 11:00 include
drwxr-xr-x  5 root root   230 Jul  4 11:00 lib
-rw-r--r--  1 7161 31415 250K Dec  8  2022 LICENSE
drwxr-xr-x  4 root root    30 Jul  4 11:00 man
-rw-r--r--  1 7161 31415  566 Dec  8  2022 README
drwxr-xr-x 28 root root  4.0K Jul  4 11:00 share
drwxr-xr-x  2 root root    90 Jul  4 11:00 support-files

把文件放到/usr/local/mysql目录下

[root@mysql-mgr-1 opt]# mv mysql-5.7.41-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql-mgr-1 opt]# ll -h /usr/local/mysql/
total 268K
drwxr-xr-x  2 root root  4.0K Jul  4 11:00 bin
drwxr-xr-x  2 root root    73 Jul  4 11:00 docs
drwxr-xr-x  3 root root  4.0K Jul  4 11:00 include
drwxr-xr-x  5 root root   230 Jul  4 11:00 lib
-rw-r--r--  1 7161 31415 250K Dec  8  2022 LICENSE
drwxr-xr-x  4 root root    30 Jul  4 11:00 man
-rw-r--r--  1 7161 31415  566 Dec  8  2022 README
drwxr-xr-x 28 root root  4.0K Jul  4 11:00 share
drwxr-xr-x  2 root root    90 Jul  4 11:00 support-files

创建数据库运行用户

[root@mysql-mgr-1 opt]# groupadd mysql
[root@mysql-mgr-1 opt]# useradd -g mysql -s /sbin/nologin mysql
[root@mysql-mgr-1 opt]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)

配置PATH

[root@mysql-mgr-1 opt]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile.d/mysql.sh
[root@mysql-mgr-1 opt]# . /etc/profile
[root@mysql-mgr-1 opt]# echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf
[root@mysql-mgr-1 opt]# ldconfig
[root@mysql-mgr-1 opt]# ln -s /usr/local/mysql/bin/my*  /usr/bin

数据库目录规划

文件类型 实例3306 软链
数据datadir /usr/local/mysql/data /data/mysql/data
参数文件my.cnf /usr/local/mysql/etc/my.cnf /data/mysql/etc
错误日志log-error /usr/local/mysql/log/mysql_error.log /data/mysql/log
二进制日志log-bin /usr/local/mysql/binlogs/mysql-bin /data/mysql/binlogs/mysql-bin
慢查询日志slow_query_log_file /usr/local/mysql/log/mysql_slow_query.log /data/mysql/log
套接字socket文件 /usr/local/mysql/run/mysql.sock /data/mysql/run
pid文件 /usr/local/mysql/run/mysql.pid /data/mysql/run
[root@mysql-mgr-1 opt]# mkdir -p /data/mysql/{data,binlogs,log,etc,run}
[root@mysql-mgr-1 opt]# ln -s /data/mysql/data    /usr/local/mysql/data
[root@mysql-mgr-1 opt]# ln -s /data/mysql/binlogs    /usr/local/mysql/binlogs
[root@mysql-mgr-1 opt]# ln -s /data/mysql/log    /usr/local/mysql/log
[root@mysql-mgr-1 opt]# ln -s /data/mysql/etc    /usr/local/mysql/etc
[root@mysql-mgr-1 opt]# ln -s /data/mysql/run    /usr/local/mysql/run
[root@mysql-mgr-1 opt]# chown -R mysql.mysql     /data/mysql/
[root@mysql-mgr-1 opt]# chown -R mysql.mysql     /usr/local/mysql/{data,binlogs,log,etc,run}
[root@mysql-mgr-1 opt]# ll -h /usr/local/mysql/
total 268K
drwxr-xr-x  2 root  root  4.0K Jul  4 11:00 bin
lrwxrwxrwx  1 mysql mysql   19 Jul  4 11:07 binlogs -> /data/mysql/binlogs
lrwxrwxrwx  1 mysql mysql   16 Jul  4 11:07 data -> /data/mysql/data
drwxr-xr-x  2 root  root    73 Jul  4 11:00 docs
lrwxrwxrwx  1 mysql mysql   15 Jul  4 11:07 etc -> /data/mysql/etc
drwxr-xr-x  3 root  root  4.0K Jul  4 11:00 include
drwxr-xr-x  5 root  root   230 Jul  4 11:00 lib
-rw-r--r--  1  7161 31415 250K Dec  8  2022 LICENSE
lrwxrwxrwx  1 mysql mysql   15 Jul  4 11:07 log -> /data/mysql/log
drwxr-xr-x  4 root  root    30 Jul  4 11:00 man
-rw-r--r--  1  7161 31415  566 Dec  8  2022 README
lrwxrwxrwx  1 mysql mysql   15 Jul  4 11:07 run -> /data/mysql/run
drwxr-xr-x 28 root  root  4.0K Jul  4 11:00 share
drwxr-xr-x  2 root  root    90 Jul  4 11:00 support-files

配置mysql

删除系统自带的my.cnf

[root@mysql-mgr-1 opt]# rm /etc/my.cnf -f

/usr/local/mysql/etc/下创建my.cnf文件,加入如下参数,其他参数根据需要配置
该配置文件不适用于生产环境,仅用于实验

[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
[mysqld]
port = 3306
user=mysql
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
report_host=mysql-mgr-1
gtid_mode = on
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
binlog_format=ROW
default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535
skip-name-resolve
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 2048M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
key_buffer_size = 64M
log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/binlogs/mysql-bin
log_bin_index = mysql-bin.index
relay-log=relay-bin
relay-log-index=relay-bin.index
log_slave_updates=1
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0
query_cache_size = 0
server-id=131
您暂时无权查看此隐藏内容!
binlog_checksum = NONE default_password_lifetime=0 binlog_cache_size = 2M innodb_open_files = 4096 table_definition_cache = 4096 table_open_cache_instances = 128 thread_cache_size = 64 innodb_online_alter_log_max_size=100M

为了确保命令行能正常使用,我们需要将配置文件软链到默认位置

[root@mysql-mgr-1 opt]# ln -s /data/mysql/etc/my.cnf /etc/my.cnf

设置开机启动项

首先让systemd能管理mysql
/usr/lib/systemd/system创建mysqld.service文件。

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/run/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

启用开机启动

[root@mysql-mgr-1 opt]# systemctl daemon-reload
[root@mysql-mgr-1 opt]# systemctl enable mysqld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@mysql-mgr-1 opt]# systemctl is-enabled mysqld
enabled

初始化数据库

[root@mysql-mgr-1 opt]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

在日志文件里会提示一个临时密码,记录这个密码

[root@mysql-mgr-1 opt]# grep 'temporary password' /usr/local/mysql/log/mysql_error.log
2023-07-04T03:54:43.499513Z 1 [Note] A temporary password is generated for root@localhost: d3Es;z038tAj

生成SSL

[root@mysql-mgr-1 opt]# mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

启动MYSQL

[root@mysql-mgr-1 opt]# systemctl start mysqld.service
[root@mysql-mgr-1 opt]# netstat -lntp|grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      3419/mysqld

Securing the Initial MySQL Accounts

[root@mysql-mgr-1 opt]# 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: 

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: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... 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!

Populating the Time Zone Tables

导入时区信息

[root@mysql-mgr-1 opt]# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -pAa@111111 mysql

测试

[root@mysql-mgr-1 opt]# mysqladmin version -uroot -p
Enter password: 
mysqladmin  Ver 8.42 Distrib 5.7.41, for linux-glibc2.12 on x86_64
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.

Server version      5.7.41-log
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /usr/local/mysql/run/mysql.sock
Uptime:         5 min 38 sec

Threads: 1  Questions: 8720  Slow queries: 0  Opens: 123  Flush tables: 1  Open tables: 43  Queries per second avg: 25.798

配置日志自动轮转

[root@mysql-mgr-1 ~]# cat .my.cnf 
[mysqladmin]
password = Aa@111111
user= root

[root@mysql-mgr-1 ~]# chmod 600 .my.cnf
[root@mysql-mgr-1 ~]# ll .my.cnf 
-rw------- 1 root root 45 Jul  4 12:08 .my.cnf

复制配置文件

[root@mysql-mgr-1 ~]# cp  /usr/local/mysql/support-files/mysql-log-rotate /etc/logrotate.d/
[root@mysql-mgr-1 ~]# chmod 644 /etc/logrotate.d/mysql-log-rotate
[root@mysql-mgr-1 ~]# cat /etc/logrotate.d/mysql-log-rotate 
# The log file name and location can be set in
# /etc/my.cnf by setting the "log-error" option
# in either [mysqld] or [mysqld_safe] section as
# follows:
#
# [mysqld]
# log-error=/usr/local/mysql/data/mysqld.log
#
# In case the root user has a password, then you
# have to create a /root/.my.cnf configuration file
# with the following content:
#
# [mysqladmin]
# password = <secret> 
# user= root
#
# where "<secret>" is the password. 
#
# ATTENTION: The /root/.my.cnf file should be readable
# _ONLY_ by root !

/usr/local/mysql/log/mysql_error.log {
        # create 600 mysql mysql
        notifempty
        daily
        rotate 52
        missingok
        compress
    postrotate
    # just if mysqld is really running
    if test -x /usr/local/mysql/bin/mysqladmin && \
       /usr/local/mysql/bin/mysqladmin ping &>/dev/null
    then
       /usr/local/mysql/bin/mysqladmin flush-logs
    fi
    endscript
}

测试日志轮转

[root@mysql-mgr-1 ~]# /usr/sbin/logrotate -fv /etc/logrotate.d/mysql-log-rotate

至此安装结束,下面开始配置MGR

配置MGR

在引导节点执行:

SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;

您暂时无权查看此隐藏内容!

检查集群状态

SELECT * FROM performance_schema.replication_group_members;

其他节点也执行加入节点

SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;

您暂时无权查看此隐藏内容!

集群状态

+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 82228a6b-1a1e-11ee-af6a-000c297f3fa4 | mysql-mgr-1 |        3306 | ONLINE       |
| group_replication_applier | 822c6450-1a1e-11ee-863a-000c29abdd0a | mysql-mgr-3 |        3306 | ONLINE       |
| group_replication_applier | 8237ead4-1a1e-11ee-adda-000c290e1a93 | mysql-mgr-2 |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
内容查看本文隐藏内容查看需要消耗10土豆币,请先
土豆币按需购买,不退换,请考虑清楚后购买。
赞(0) 打赏
未经允许不得转载:陈桂林博客 » 基于MYSQL5.7.41部署MGR集群(标准作业手册)
分享到

大佬们的评论 1

  1. #1

    顶起来

    飞天茅台王子9个月前 (07-30)回复

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册