MySQL多实例部署

多实例部署MySQL

下载MySQL软件包

[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

创建MySQL用户

[root@localhost src]# useradd -M -s /sbin/nologin mysql

创建软连接

[root@localhost src]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql

修改目录/usr/src/mysql的属主属组

[root@localhost src]# ll /usr/src/mysql
lrwxrwxrwx 1 mysql mysql 36 7月  03 22:03 /usr/src/mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/

配置环境变量

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

创建各实例数据库存放达到目录

[root@localhost data]# mkdir -p /opt/data/{3306,3307,3308}
修改/opt/data目录的属主和属主
[root@localhost data]# chown -R mysql.mysql /opt/data/

初始化各个实例
3306实例初始化

[root@localhost data]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql 
2022-07-03T22:40:10.553518Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T22:40:11.174129Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T22:40:11.315106Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T22:40:11.334046Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3285bb8e-0747-11ec-8d24-000c29b869b7.
2022-07-03T22:40:11.335272Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T22:40:11.938447Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T22:40:12.292856Z 1 [Note] A temporary password is generated for root@localhost: sf1P%DNitRwl
[root@node2 data]# echo 'sf1P%DNitRwl' > 3306_password

3307实例初始化

[root@localhost data]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql 
2022-07-03T23:07:06.328966Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T23:07.815655Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T23:07:06.889250Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T23:07:06.900944Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7767d7bf-0747-11ec-b755-000c29b869b7.
2022-07-03T23:07:06.901943Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T23:07:08.200956Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T23:07:08.298757Z 1 [Note] A temporary password is generated for root@localhost: s?aIZf-sV2l>
[root@localhost data]# echo 's?aIZf-sV2l>' > 3307_password

3308实例初始化

[root@localhost data]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql 
2022-07-03T23:08:26.715767Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T23:08:27.193179Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T23:08:27.275084Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T23:08:27.290861Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: a7525ee4-0747-11ec-ab93-000c29b869b7.
2022-07-03T23:08:27.291757Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T23:08:28.159963Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T23:08:28.256708Z 1 [Note] A temporary password is generated for root@localhost: (J?siJDh(9hu
[root@localhost data]# echo '(J?siJDh(9hu' > 3308_password

安装Perl

[root@localhost data]# yum -y install perl
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:39:09 前,执行于 2022年07月03日 星期五 23时10分33秒。
软件包 perl-4:5.26.3-416.el8.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!

// 配置mysql的配置文件

[mysqld_multi]
mysqld = /usr/src/mysql/bin/mysqld_safe
mysqladmin = /usr/src/mysql/bin/mysqladmin

[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log_error = /var/log/3306.log

[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log_error = /var/log/3307.log

[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3308/mysql_3308.pid
log_error = /var/log/3308.log

// 启动各个实例

[root@localhost ~]# mysqld_multi start 3306
Wide character in print at /usr/src/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# mysqld_multi start 3307
Wide character in print at /usr/src/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# mysqld_multi start 3308
Wide character in print at /usr/src/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# ss -anlt
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0         128                     [::]:22                   [::]:*       
LISTEN    0         80                         *:3306                    *:*       
LISTEN    0         80                         *:3307                    *:*       
LISTEN    0         80                         *:3308                    *:* 

初始化各个数据库的密码
3307和3308数据库同下

若进入数据库失败请安装libncurses*
yum install libncurses*
[root@localhost ~]# mysql -uroot -p'sf1P%DNitRwl' -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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

设置开启自启

[root@localhost init.d]# cp /usr/lib/systemd/system/sshd.service ./mysqld.service
[root@localhost init.d]# vim mysqld.service
[root@localhost init.d]# mv ./mysqld.service /usr/lib/systemd/system/
[root@localhost init.d]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysqld server daemon
After=network.target

[Service]
Type=notify
ExecStart=/usr/src/mysql/bin/mysqld_multi start
Execstop=/usr/bin/kill -KILL $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

[root@localhost support-files]# pwd
/usr/src/mysql/support-files
[root@localhost support-files]# cd /usr/src/mysql/support-files/
[root@localhost support-files]# cp -a mysqld_multi.server /etc/init.d/mysqld_multi

[rootlocalhost support-files]# vim /etc/init.d/mysqld_multi
export PATH=/usr/src/mysql/bin:$PATH
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin

[root@localhost support-files]# service mysqld_multi start 
[root@localhost support-files]# ss -anlt
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*      
LISTEN   0        80                      *:3306                  *:*      
LISTEN   0        80                      *:3307                  *:*      
LISTEN   0        80                      *:3308                  *:*      
LISTEN   0        128                  [::]:22                 [::]:*

最后添加mysqld_multi服务就可以实现开机自启
[root@localhost init.d]# chkconfig --add mysqld_multi
若想关闭MySQL服务需要使用pkill mysqld命令进行关闭
手动开启的命令是:
[root@localhost init.d]# service mysqld_multi start
posted @   夏天的海  阅读(94)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示