mysql多实例部署

在MySQL中配置多实例

1.软件下载

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-29 13:28:14--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.2.84.230
Connecting to cdn.mysql.com (cdn.mysql.com)|23.2.84.230|:443... connected.

HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'

mysql-5.7.38-linux-glibc2.12-x 100%[==================================================>] 643.57M  3.35MB/s    in 4m 15s

2022-07-29 13:32:30 (2.52 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
[root@localhost src]# ls
debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

2.配置用户和组并解压二进制程序至/usr/local下

[root@localhost src]# useradd -M -r -s /sbin/nologin mysql
[root@localhost src]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd
[root@localhost ~]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.38-linux-glibc2.12-x86_64  sbin  share  src
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root   6 Jun 22  2021 bin
drwxr-xr-x. 2 root root   6 Jun 22  2021 etc
drwxr-xr-x. 2 root root   6 Jun 22  2021 games
drwxr-xr-x. 2 root root   6 Jun 22  2021 include
drwxr-xr-x. 2 root root   6 Jun 22  2021 lib
drwxr-xr-x. 3 root root  17 Jul 12 21:45 lib64
drwxr-xr-x. 2 root root   6 Jun 22  2021 libexec
lrwxrwxrwx. 1 root root  35 Jul 29 13:39 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Jul 29 13:38 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Jun 22  2021 sbin
drwxr-xr-x. 5 root root  49 Jul 12 21:45 share
drwxr-xr-x. 2 root root   6 Jun 22  2021 src

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 35 Jul 29 13:39 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64

//配置环境变量
[root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

//创建头文件
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ls
LICENSE  README  bin  docs  include  lib  man  share  support-files

[root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql     //配置软链接
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost ~]# chown -R mysql.mysql /usr/include/mysql
[root@localhost ~]# ll -d /usr/include/mysql
lrwxrwxrwx. 1 mysql mysql 25 Jul 29 14:37 /usr/include/mysql -> /usr/local/mysql/include/

[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf
#MANDATORY_MANPATH                      /usr/src/pvm3/man
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man        //添加一行

3.创建各实例数据存放的目录

[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3308
[root@localhost ~]# tree /opt/data/
/opt/data/
|-- 3306
|-- 3307
`-- 3308

4.初始化各示例

//初始化3306
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306
2022-07-29T05:48:04.206397Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2022-07-29T05:48:04.369296Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-29T05:48:04.403256Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2022-07-29T05:48:04.462960Z 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: 03b0e61e-0f02-11ed-b4ce-000c296d5362.

2022-07-29T05:48:04.464340Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2022-07-29T05:48:04.643259Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:48:04.643284Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:48:04.643585Z 0 [Warning] CA certificate ca.pem is self signed.

2022-07-29T05:48:04.717924Z 1 [Note] A temporary password is generated for root@localhost: ,dASzpwwE3RJ
[root@localhost ~]# echo ',dASzpwwE3RJ' > 3306
[root@localhost ~]# ls
3306  anaconda-ks.cfg

//初始化3307
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2022-07-29T05:49:29.037700Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2022-07-29T05:49:29.195218Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-29T05:49:29.224130Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2022-07-29T05:49:29.281903Z 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: 363f3e1c-0f02-11ed-8fdd-000c296d5362.

2022-07-29T05:49:29.283175Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2022-07-29T05:49:29.523082Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:49:29.523118Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:49:29.523503Z 0 [Warning] CA certificate ca.pem is self signed.

2022-07-29T05:49:29.581009Z 1 [Note] A temporary password is generated for root@localhost: )MhldlF<o23j [root@localhost ~]# echo ')mhldlf<o23j'> 3307
[root@localhost ~]# ls
3306  3307  anaconda-ks.cfg

//&#x521D;&#x59CB;&#x5316;3308
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2022-07-29T05:50:14.735254Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2022-07-29T05:50:14.892190Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-29T05:50:14.915269Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2022-07-29T05:50:14.973272Z 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: 517b3121-0f02-11ed-a3f6-000c296d5362.

2022-07-29T05:50:14.974697Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2022-07-29T05:50:15.340479Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:50:15.340510Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.

2022-07-29T05:50:15.341068Z 0 [Warning] CA certificate ca.pem is self signed.

2022-07-29T05:50:15.368072Z 1 [Note] A temporary password is generated for root@localhost: tkdnhiEk>5vh
[root@localhost ~]# echo 'tkdnhiEk>5vh' > 3308
[root@localhost ~]# ls
3306  3307  3308  anaconda-ks.cfg
</o23j>

5.配置配置文件/etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/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/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

6.启动各示例

[root@localhost ~]# mysqld_multi start 3306
-bash: /usr/local/mysql/bin/mysqld_multi: /usr/bin/perl: bad interpreter: No such file or directory             //&#x9700;&#x8981;&#x4E0B;&#x8F7D;perl&#x5305;
[root@localhost ~]# dnf -y install perl
[root@localhost mysql]# yum install ncurses-compat-libs
[root@localhost mysql]# file /usr/local/mysql/bin/mysqld_multi
/usr/local/mysql/bin/mysqld_multi: Perl script text executable
[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -anlt
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             80                               *:3308                          *:*
LISTEN        0             128                           [::]:22                         [::]:*
LISTEN        0             80                               *:3306                          *:*
LISTEN        0             80                               *:3307                          *:*

7.使用system控制启动

//&#x9700;&#x8981;&#x5148;&#x628A;mysql&#x670D;&#x52A1;&#x505C;&#x6B62; //mysqld_mulit &#x547D;&#x4EE4;&#x505C;&#x6B62;&#x4E0D;&#x4E86;&#x53EA;&#x80FD;&#x6740;&#x8FDB;&#x7A0B;
[root@localhost ~]# ps -ef | grep 3306 | grep -v grep
root       77016       1  0 13:55 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/3306 --port=3306 --socket=/tmp/mysql3306.sock --pid-file=/opt/data/3306/mysql_3306.pid --log-error=/var/log/3306.log
mysql      77152   77016  0 13:55 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data/3306 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/3306.log --pid-file=/opt/data/3306/mysql_3306.pid --socket=/tmp/mysql3306.sock --port=3306
[root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}'
77016
77152
[root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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             80                               *:3308                          *:*
LISTEN        0             128                           [::]:22                         [::]:*
LISTEN        0             80                               *:3307                          *:*
[root@localhost ~]# ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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             80                               *:3308                          *:*
LISTEN        0             128                           [::]:22                         [::]:*
[root@localhost ~]# ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
[root@localhost ~]# ss -anlt
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             128                           [::]:22                         [::]:*

//&#x627E;&#x4E2A;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;system&#x63A7;&#x5236;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x8FDB;&#x884C;cp&#x4E00;&#x4E0B;&#xFF0C;&#x5E76;&#x8FDB;&#x884C;&#x4FEE;&#x6539;
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service

//&#x914D;&#x7F6E;3306
[root@localhost ~]# vim /usr/lib/systemd/system/3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
ExecStop=ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//&#x914D;&#x7F6E;3307
[root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service
[root@localhost ~]# vim /usr/lib/systemd/system/3307.service    //&#x4F7F;&#x7528;:% s/6/7/g&#x4E00;&#x952E;&#x66FF;&#x6362;
[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307
ExecStop=ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//&#x914D;&#x7F6E;3308
[root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service
[root@localhost ~]# vim /usr/lib/systemd/system/3308.service     //&#x4F7F;&#x7528;:% s/6/7/g&#x4E00;&#x952E;&#x66FF;&#x6362;
[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308
ExecStop=ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//&#x5237;&#x65B0;&#x4E00;&#x4E0B;
[root@localhost ~]# systemctl daemon-reload

//&#x5173;&#x95ED;&#x9632;&#x706B;&#x5899;&#x548C;selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled

//&#x542F;&#x52A8;3306&#xFF0C;3307&#xFF0C;3308
[root@localhost ~]# systemctl start 3306
Job for 3306.service failed because the control process exited with error code.

See "systemctl status 3306.service" and "journalctl -xe" for details.  //&#x6CA1;&#x6709;&#x627E;&#x5230;my_print_defaults&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;

//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8F6F;&#x94FE;&#x63A5;&#x5230;/usr/bin&#x4E0B;
[root@localhost ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
[root@localhost ~]# systemctl start 3306
[root@localhost ~]# systemctl start 3307
[root@localhost ~]# systemctl start 3308
[root@localhost ~]# ss -anlt
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             80                               *:3308                          *:*
LISTEN        0             128                           [::]:22                         [::]:*
LISTEN        0             80                               *:3306                          *:*
LISTEN        0             80                               *:3307                          *:*

8.初始化密码

//3306
[root@localhost ~]# mysql -uroot -p',dASzpwwE3RJ' -S /tmp/mysql3306.sock
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//3307
[root@localhost ~]# mysql -uroot -p')MhldlF<o23j' -s tmp mysql3307.sock mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//3308
[root@localhost ~]# mysql -uroot -p'tkdnhiEk>5vh' -S /tmp/mysql3308.sock
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
</o23j'>

Original: https://www.cnblogs.com/Their-own/p/16532585.html
Author: 事愿人为
Title: mysql多实例部署

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505123/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球