栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MySQL多实例部署

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL多实例部署

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

文章目录
    • 1. 配置用户和组并解压二进制程序至/usr/local下
      • 1.2创建各实例数据存放的目录
      • 1.3初始化各实例
      • 1.4安装perl
      • 1.5配置配置文件/etc/my.cnf
      • 1.6启动各实例
      • 1.7修改所有密码
      • 1.8 设置开机自启

//创建MySQL用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug  kernels  mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
//解压安装包并放到/usr/local/下
[root@localhost src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
//重命名
[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 8月  12 2018 bin
drwxr-xr-x. 2 root root   6 8月  12 2018 etc
drwxr-xr-x. 2 root root   6 8月  12 2018 games
drwxr-xr-x. 2 root root   6 8月  12 2018 include
drwxr-xr-x. 2 root root   6 8月  12 2018 lib
drwxr-xr-x. 2 root root   6 8月  12 2018 lib64
drwxr-xr-x. 2 root root   6 8月  12 2018 libexec
drwxr-xr-x. 9 root root 129 7月   3 02:38 mysql
drwxr-xr-x. 2 root root   6 8月  12 2018 sbin
drwxr-xr-x. 5 root root  49 5月  24 10:16 share
drwxr-xr-x. 2 root root   6 8月  12 2018 src
//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll 
总用量 0
drwxr-xr-x. 2 root  root    6 8月  12 2018 bin
drwxr-xr-x. 2 root  root    6 8月  12 2018 etc
drwxr-xr-x. 2 root  root    6 8月  12 2018 games
drwxr-xr-x. 2 root  root    6 8月  12 2018 include
drwxr-xr-x. 2 root  root    6 8月  12 2018 lib
drwxr-xr-x. 2 root  root    6 8月  12 2018 lib64
drwxr-xr-x. 2 root  root    6 8月  12 2018 libexec
drwxr-xr-x. 9 mysql mysql 129 7月   3 02:38 mysql
drwxr-xr-x. 2 root  root    6 8月  12 2018 sbin
drwxr-xr-x. 5 root  root   49 5月  24 10:16 share
drwxr-xr-x. 2 root  root    6 8月  12 2018 src
//配置环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# chmod g+x /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1.2创建各实例数据存放的目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 7月   3 02:53 3306
drwxr-xr-x. 2 mysql mysql 6 7月   3 02:53 3307
drwxr-xr-x. 2 mysql mysql 6 7月   3 02:53 3308

1.3初始化各实例
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/3306
2022-07-03T07:04:34.756080Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T07:04:35.016633Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T07:04:35.054608Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T07:04:35.110979Z 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: 6530b20a-fa9e-11ec-a19e-000c292f729f.
2022-07-03T07:04:35.111886Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T07:04:35.636584Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:04:35.636628Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:04:35.637127Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T07:04:35.797625Z 1 [Note] A temporary password is generated for root@localhost: jiA>eQCp9Ucp
[root@localhost ~]# echo 'jiA>eQCp9Ucp' 3306_pass
jiA>eQCp9Ucp 3306_pass
[root@localhost ~]# echo 'jiA>eQCp9Ucp' > 3306_pass
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/3307
2022-07-03T07:05:28.820084Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T07:05:29.067066Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T07:05:29.106147Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T07:05:29.162420Z 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: 85684a98-fa9e-11ec-b8a5-000c292f729f.
2022-07-03T07:05:29.163379Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T07:05:29.688529Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:05:29.688577Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:05:29.689022Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T07:05:29.831549Z 1 [Note] A temporary password is generated for root@localhost: sA.S+ynkl2*5
[root@localhost ~]# echo 'sA.S+ynkl2*5' > 3307_pass
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/3308
2022-07-03T07:06:07.387711Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T07:06:07.617685Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T07:06:07.654159Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T07:06:07.710696Z 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: 9c624970-fa9e-11ec-b622-000c292f729f.
2022-07-03T07:06:07.711872Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T07:06:08.125634Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:06:08.125671Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T07:06:08.126327Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T07:06:08.602522Z 1 [Note] A temporary password is generated for root@localhost: SX)r7r3aqYqt
[root@localhost ~]# echo 'SX)r7r3aqYqt' > 3308_pass

1.4安装perl
[root@localhost ~]# yum -y install perl
1.5配置配置文件/etc/my.cnf
[root@localhost ~]# vim /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

1.6启动各实例
[root@localhost data]# mysqld_multi start 3306
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost data]# mysqld_multi start 3307
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost data]# mysqld_multi start 3308
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost data]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN     0          128                  0.0.0.0:111               0.0.0.0:*       
LISTEN     0          32             192.168.122.1:53                0.0.0.0:*       
LISTEN     0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN     0          5                  127.0.0.1:631               0.0.0.0:*       
LISTEN     0          80                         *:3306                    *:*       
LISTEN     0          80                         *:3307                    *:*       
LISTEN     0          80                         *:3308                    *:*       
LISTEN     0          128                     [::]:111                  [::]:*       
LISTEN     0          128                     [::]:22                   [::]:*       
LISTEN     0          5                      [::1]:631                  [::]:*    
1.7修改所有密码
[root@localhost data]# mysql -uroot -p'igco1Zg)sONr' -S /tmp/mysql3306.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@localhost data]# cat 3307_pass 
UioEzV!_G148
[root@localhost data]# mysql -uroot -p'UioEzV!_G148' -S /tmp/mysql3307.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@localhost data]# cat 3308_pass 
e>Q-oqqaO1kf
[root@localhost data]# mysql -uroot -p'e>Q-oqqaO1kf' -S /tmp/mysql3308.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

1.8 设置开机自启
[root@localhost ~]# cd /usr/lib/systemd/system/
[root@localhost system]# cp sshd.service 3306.service
[root@localhost system]# vim 3306.service 

[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target
Wants=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

[root@localhost system]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/
[root@localhost system]# systemctl start 3306.service
[root@localhost system]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN      0           128                    0.0.0.0:111                 0.0.0.0:*        
LISTEN      0           32               192.168.122.1:53                  0.0.0.0:*        
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*        
LISTEN      0           80                           *:3306                      *:*        
LISTEN      0           128                       [::]:111                    [::]:*        
LISTEN      0           128                       [::]:22                     [::]:*        
LISTEN      0           5                        [::1]:631                    [::]:*        
[root@localhost system]# systemctl status 3306.service 
● 3306.service - 3306 server daemon
   Loaded: loaded (/usr/lib/systemd/system/3306.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-07-03 04:45:46 EDT; 25s ago
  Process: 118360 ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306 (code=exited, stat>
 Main PID: 118365 (mysqld_safe)
    Tasks: 28 (limit: 11160)
   Memory: 190.0M
   CGroup: /system.slice/3306.service
           ├─118365 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/3306 --por>
           └─118504 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/d>

7月 03 04:45:46 localhost.localdomain systemd[1]: Starting 3306 server daemon...
7月 03 04:45:46 localhost.localdomain mysqld_multi[118360]: Wide character in print at /usr>
7月 03 04:45:46 localhost.localdomain systemd[1]: Started 3306 server daemon.
[root@localhost system]# systemctl enable --now 3306.service 
Created symlink /etc/systemd/system/multi-user.target.wants/3306.service → /usr/lib/systemd/system/3306.service.
[root@localhost system]# 
//3307开机自启
[root@localhost system]# cp 3306.service 3307.service
[root@localhost system]# vim 3307.service 

[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target
Wants=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

//3308开机自启
[root@localhost system]# cp 3306.service 3308.service
[root@localhost system]# vim 3308.service 

[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target
Wants=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


查看能能否通过服务启动
[root@localhost system]# systemctl start 3307.service 
[root@localhost system]# systemctl start 3308.service 
[root@localhost system]# systemctl enable --now 3307.service 
Created symlink /etc/systemd/system/multi-user.target.wants/3307.service → /usr/lib/systemd/system/3307.service.
[root@localhost system]# systemctl enable --now 3308.service 
Created symlink /etc/systemd/system/multi-user.target.wants/3308.service → /usr/lib/systemd/system/3308.service.
[root@localhost system]# ss -antl                     //可以看到已经全部启动了
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN      0           128                    0.0.0.0:111                 0.0.0.0:*        
LISTEN      0           32               192.168.122.1:53                  0.0.0.0:*        
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*        
LISTEN      0           80                           *:3306                      *:*        
LISTEN      0           80                           *:3307                      *:*        
LISTEN      0           80                           *:3308                      *:*        
LISTEN      0           128                       [::]:111                    [::]:*        
LISTEN      0           128                       [::]:22                     [::]:*        
LISTEN      0           5                        [::1]:631                    [::]:*    
  • 这样就相当于一台服务器安装了三个独立的mysql
[root@localhost system]# mysql -uroot -p'runtime123!' -S /tmp/mysql3307.sock
mysql> create database node;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| node               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> quit
Bye
[root@localhost system]# 

//用3308查看
[root@localhost system]# mysql -uroot -p'runtime123!' -S /tmp/mysql3308.sock
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
//可以看出它们都是独立的个体
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002956.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号