mysql的多实例配置

Posted rmㅤ-rf *

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql的多实例配置相关的知识,希望对你有一定的参考价值。

mysql的多实例配置

下载安装包

[root@localhost mysql]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
--2021-05-04 20:34:21--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz [following]
--2021-05-04 20:34:23--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 223.119.236.209
Connecting to cdn.mysql.com (cdn.mysql.com)|223.119.236.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 661718255 (631M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz’
mysql-5.7.33-linux- 100%[================>] 631.06M  11.3MB/s    in 57s     
2021-05-04 20:35:21 (11.0 MB/s) - ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz’ saved [661718255/661718255]

创建用户

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

解压至指定目录

[root@localhost mysql]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local

创建软链接或者修改目录名

[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src

修改属主和属组

[root@localhost local]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost local]# ll
drwxr-xr-x.  9 mysql mysql 129 五月    4 20:40 mysql

设置环境变量(因为不是用yum装的,找不到mysql程序)

[root@localhost mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost mysql]# . /etc/profile.d/mysql.sh
[root@localhost mysql]# which mysql
/usr/local/mysql/bin/mysql

创建存放数据的目录并修改属主属组

需要一个空间大的目录,或者将目录创建后将硬盘设备挂载在此目录上

[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# mkdir /opt/data/3306
[root@localhost ~]# mkdir /opt/data/3307
[root@localhost ~]# mkdir /opt/data/3308
[root@localhost mysql]# chown -R mysql.mysql /opt/mysql_data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 五月   10 17:52 3306
drwxr-xr-x. 2 mysql mysql 6 五月   10 17:52 3307
drwxr-xr-x. 2 mysql mysql 6 五月   10 17:52 3308

初始化各实例

[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306/ --user=mysql
2021-05-10T09:54:29.434788Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T09:54:29.642830Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T09:54:29.699130Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T09:54:29.709396Z 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: b68fb52e-b175-11eb-9097-000c294bb269.
2021-05-10T09:54:29.711278Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T09:54:30.890901Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T09:54:31.065608Z 1 [Note] A temporary password is generated for root@localhost: s0&Cbmj.w(rF
[root@localhost ~]# vim 3306
s0&Cbmj.w(rF
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307/ --user=mysql
2021-05-10T09:55:51.206329Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T09:55:51.421385Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T09:55:51.470945Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T09:55:51.479599Z 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: e74cdb61-b175-11eb-931b-000c294bb269.
2021-05-10T09:55:51.480475Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T09:55:52.517167Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T09:55:53.142665Z 1 [Note] A temporary password is generated for root@localhost: SjXffRqy+2OU
[root@localhost ~]# vim 3307
SjXffRqy+2OU
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308/ --user=mysql
2021-05-10T09:56:13.679400Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T09:56:13.903692Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T09:56:13.943950Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T09:56:13.951038Z 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: f4b1ba07-b175-11eb-95d1-000c294bb269.
2021-05-10T09:56:13.951976Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T09:56:15.032285Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T09:56:15.319902Z 1 [Note] A temporary password is generated for root@localhost: he2,z&wy;eYd
[root@localhost ~]# vim 3308
he2,z&wy;eYd

修改配置文件

[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-dile=/opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
[mysqld3307]
datadir=/opt/data/3307
port=3307
socket=/tmp/mysql3307.sock
pid-dile=/opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log
[mysqld3308]
datadir=/opt/data/3308
port=3308
socket=/tmp/mysql3308.sock
pid-dile=/opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

启动各实例

[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       128                   *:443                 *:*              
LISTEN  0       80                    *:3306                *:*              
LISTEN  0       80                    *:3307                *:*              
LISTEN  0       80                    *:3308                *:*              
LISTEN  0       128                   *:80                  *:*              
LISTEN  0       128                [::]:22               [::]:*              

启动服务并修改密码

[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       128                   *:443                 *:*              
LISTEN  0       80                    *:3306                *:*              
LISTEN  0       80                    *:3307                *:*              
LISTEN  0       80                    *:3308                *:*              
LISTEN  0       128                   *:80                  *:*              
LISTEN  0       128                [::]:22               [::]:*              
[root@localhost ~]# mysql -uroot -p's0&Cbmj.w(rF' -h127.0.0.1
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.33

Copyright (c) 2000, 2021, 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.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> set password = password('hanao.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# cat 3307
SjXffRqy+2OU
[root@localhost ~]# mysql -uroot -p'SjXffRqy+2OU' -S /tmp/mysql3307.sock
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 3
Server version: 5.7.33

Copyright (c) 2000, 2021, 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.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> set password = password('hanao.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# cat 3308
he2,z&wy;eYd
[root@localhost ~]# mysql -uroot -p'he2,z&wy;eYd' -S /tmp/mysql3308.sock
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.33

Copyright (c) 2000, 2021, 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.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> set password = password('hanao.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye

配置开机自启

[root@localhost ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/
[root@localhost ~]# vim /etc/init.d/mysqld_multi.server 
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost ~]# chkconfig mysqld_multi.server on
[root@localhost ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld_multi.server	0:off	1:off	2:on	3:on	4:on	5:on	6:off

以上是关于mysql的多实例配置的主要内容,如果未能解决你的问题,请参考以下文章

三linux-mysql mysql的多实例

理想的多开发商灯组?

如何为 XSLT 代码片段配置 CruiseControl 的 C# 版本?

Mysql------多实例配置

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

mysql多实例配置