目录
1.配置文件配置多实例参数
在/etc/my.cnf中加入
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe #启动多实例mysql时的命令,也可以用/usr/local/mysql/bin/mysqld,但是一般用mysqld_safe,会多一个守护进程
mysqladmin=/usr/local/mysql/bin/mysqladmin #关闭多实例mysql时用的命令,需要用户名和密码
log=/usr/local/mysql/mysqld_multi.log
[mysqld1]
port =3307
datadir= /mysql1_data
socket = /tmp/mysql.sock1
[root@localhost ~]# cat /etc/my.cnf
[mysql]
prompt=(\\u@\\h) [\\d]>\\_
[mysqld]
port =3306
user = mysql
datadir = /mysql_data
log_error = error.log
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /usr/local/mysql/mysqld_multi.log
user = root
pass = 123456
[mysqld1]
port = 3307
datadir = /mysql1_data
socket = /tmp/mysql.sock1
2.初始化第2个实例(无密码)
mysqld --initialize-insecure --datadir=/mysql1_data
[root@localhost /]# mysqld --initialize-insecure --datadir=/mysql1_data
3.启动第2个实例
mysqld_multi start 1命令中的1代表my.cnf中配置的[mysqld1]的1
[root@localhost ~]# mysqld_multi start 1
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
查看端口
[root@localhost ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
tcp6 0 0 ::1:6011 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::3307 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
4.未找到实例报错
[root@localhost mysql]# mysqld_multi report
Reporting MySQL servers
No groups to be reported (check your GNRs)
是因为我在配置文件中的参数写错了,[mysqld1]写成[mysql1]了
5.配置第3个实例
[root@localhost ~]# cat /etc/my.cnf
[mysql]
prompt=(\\u@\\h) [\\d]>\\_
[mysqld]
port =3306
user = mysql
datadir = /mysql_data
log_error = error.log
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /usr/local/mysql/mysqld_multi.log
[mysqld1]
port = 3307
datadir = /mysql1_data
socket = /tmp/mysql.sock1
[mysqld2]
port = 3308
datadir = /mysql2_data
socket = /tmp/mysql.sock2
6.启动第3个实例
[root@localhost ~]# mysqld_multi start 2
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
查看第3个示例端口
[root@localhost ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
tcp6 0 0 ::1:6011 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::3307 :::* LISTEN
tcp6 0 0 :::3308 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
7.停止多实例
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
[root@localhost mysql1_data]# mysqld_multi stop
[root@localhost mysql1_data]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
8.多实例停不下来处理
需要注意如果不能停止多实例,把mysql配置文件中[mysqld_multi]下的password = 123456改成pass = 123456就可以正常停止
或者
需修改mysqld_multi 代码,把
my $com= join ' ', 'my_print_defaults ', @defaults_options, $group;
替换为:
my $com= join ' ', 'my_print_defaults -s', @defaults_options, $group;
其中增加了一个-s
并且需要我们做一个授权
(root@not_connected) [mysql]> grant shutdown on *.* to 'username'@'localhost' identified by '123456'
另外还需要在my.cnf配置文件中加上:
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /usr/local/mysql/mysqld_multi.log
user = root
password = password
或者
[client]
user=root
password=123456
9.设置启动关闭服务
[root@localhost support-files]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/
[root@localhost support-files]# /etc/init.d/mysqld_multi.server stop
[root@localhost support-files]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
[root@localhost support-files]# mysqld_multi start
[root@localhost support-files]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
mysql是通过mysqld程序来启动的,启动进入mysql后,可以通过shutdown命令关闭
[root@localhost mysql1_data]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
[root@localhost mysql1_data]# mysql -S/tmp/mysql.sock1 -p123456
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.44 MySQL Community Server (GPL)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> shutdown
-> ;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [(none)]> exit
Bye
[root@localhost mysql1_data]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is running
[root@localhost mysql1_data]# mysqladmin -S/tmp/mysql.sock2 -uroot -p123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql1_data]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
mysql_safe是mysql守护进程,检测mysql进程是否存在,不存在就启动mysql进程
[root@localhost mysql1_data]# ps -ef | grep mysql
root 1326 1 0 10:13 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mysql_data --pid-file=/mysql_data/localhost.localdomain.pid
mysql 1468 1326 0 10:13 ? 00:01:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=/mysql_data/localhost.localdomain.pid --port=3306
root 10494 2407 0 13:33 pts/0 00:00:00 grep --color=auto mysql
[root@localhost mysql1_data]# kill -9 1468
[root@localhost mysql1_data]# ps -ef | grep mysql
root 1326 1 0 10:13 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mysql_data --pid-file=/mysql_data/localhost.localdomain.pid
mysql 10519 1326 12 13:34 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=/mysql_data/localhost.localdomain.pid --port=3306
root 10549 2407 0 13:34 pts/0 00:00:00 grep --color=auto mysql
/etc/init.d/mysql.server stop命令是安全关闭,其中kill -0是发送给mysql一个信号,我要关闭你了
10.忘记密码处理
如果忘记mysql的用户密码可以在配置文件中加上参数skip_grant_tables,然后更新mysql数据库中user表的authentication_string字段
[root@localhost mysql1_data]# /etc/init.d/mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@localhost mysql1_data]# /etc/init.d/mysql.server start
Starting MySQL. SUCCESS!
[root@localhost mysql1_data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> set password = '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
(root@localhost) [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
(root@localhost) [mysql]> select Host,User,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| Host | User | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| % | ame | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| % | lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-----------+---------------+-------------------------------------------+
(root@localhost) [mysql]> update user set authentication_string=password('1234567') where User='root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
(root@localhost) [mysql]> flush privileges;
Query OK, 0 row affected (0.00 sec)
另一个窗口,my.cnf中屏蔽掉skip_grant_tables后重启mysql,密码修改成功
[root@localhost support-files]# /etc/init.d/mysql.server restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost support-files]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost support-files]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost support-files]# mysql -uroot -p1234567
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 4
Server version: 5.7.44 MySQL Community Server (GPL)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]>
11.安装多版本mysql
11.1安装mysql5.7.32
-
创建数据目录/mysql32_data
-
修改属组为mysql:mysql
-
在my.cnf中加入另一个版本的参数
-
做好软连接:ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql32
-
初始化该mysql:bin/mysqld --initialize-insecure --datadir=/mysql32_data
-
启动该mysql实例:mysqld_multi start 32
[root@localhost local]# mkdir /mysql32_data
[root@localhost local]# chown mysql:mysql /mysql32_data
[root@localhost local]# ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql32
[root@localhost local]# cat /etc/my.cnf
[mysqld32]
server_id = 32
port = 3332
basedir = /usr/local/mysql32
datadir = /mysql32_data
socket = /tmp/mysql.sock32
[root@localhost mysql32]# bin/mysqld --initialize-insecure --datadir=/mysql32_data
[root@localhost mysql32]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is not running
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
[root@localhost mysql32]# mysqld_multi start 32
[root@localhost mysql32]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is running
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
[root@localhost mysql32_data]# ps -ef | grep mysql
root 11465 1 0 13:56 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mysql_data --pid-file=/mysql_data/localhost.localdomain.pid
mysql 11605 11465 0 13:56 pts/1 00:00:17 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=/mysql_data/localhost.localdomain.pid --port=3306
root 12282 1 0 14:27 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3307 --datadir=/mysql1_data --socket=/tmp/mysql.sock1
mysql 12437 12282 0 14:27 pts/0 00:00:07 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysql1_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock1 --port=3307
root 12720 1 0 14:29 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3308 --datadir=/mysql2_data --socket=/tmp/mysql.sock2
mysql 12875 12720 0 14:29 pts/0 00:00:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysql2_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock2 --port=3308
root 13872 1 0 14:50 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --server_id=32 --port=3332 --basedir=/usr/local/mysql32 --datadir=/mysql32_data --socket=/tmp/mysql.sock32
mysql 14056 13872 3 14:50 pts/1 00:00:00 /usr/local/mysql32/bin/mysqld --basedir=/usr/local/mysql32 --datadir=/mysql32_data --plugin-dir=/usr/local/mysq32/lib/plugin --user=mysql --server-id=32 --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock32 --port=3332
root 14105 2921 0 14:50 pts/1 00:00:00 grep --color=auto mysql
11.2安装mysql8.0.35
-
解压mysql-8.0.35-linux-glibc2.12-x86_64.tar.xz至/usr/local/
-
创建数据目录/mysql80_data
-
修改数据目录属组:chown mysql:mysql /mysql80_data
-
做好软连接:ln -s /usr/local/mysql-8.0.35-linux-glibc2.12-x86_64 mysql80
-
在my.cnf中加入8.0版本的参数
-
初始化8.0版本mysql:bin/mysqld --initialize --datadir=/mysql80_data
-
启动该mysql实例:mysqld_multi start 80
[root@localhost local]# tar xf mysql-8.0.35-linux-glibc2.12-x86_64.tar.xz
[root@localhost local]# mkdir /mysql80_data
[root@localhost local]# chown mysql:mysql /mysql80_data
[root@localhost local]# ln -s /usr/local/mysql-8.0.35-linux-glibc2.12-x86_64 mysql80
[root@localhost local]# cat /etc/my.cnf
[mysqld80]
port = 3380
basedir = /usr/local/mysql80
datadir = /mysql80_data
socket = /tmp/mysql.sock80
[root@localhost local]# cd mysql80
[root@localhost mysql80]# bin/mysqld --initialize-insecure --datadir=/mysql80_data
[root@localhost mysql80]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is running
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld80 is not running
[root@localhost mysql80]# mysqld_multi start 80
[root@localhost mysql80]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is running
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld80 is running
[root@localhost mysql80]# netstat -nlt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
tcp6 0 0 ::1:6011 :::* LISTEN
tcp6 0 0 :::33060 :::* LISTEN
tcp6 0 0 :::3332 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::3307 :::* LISTEN
tcp6 0 0 :::3308 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::3380 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
需要注意在mysql5.7.4-5.7.10中default_password_lifetime参数默认是360天密码过期,5.7.10后默认密码期限为不过期
[root@localhost mysql80]# mysql -uroot -p123456
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 12
Server version: 5.7.44 MySQL Community Server (GPL)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> show variables like 'default_password_lifetime';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| default_password_lifetime | 0 |
+---------------------------+-------+
1 row in set (0.01 sec)