当前位置:   article > 正文

mysql5.7多实例

mysql5.7多实例

目录

1.配置文件配置多实例参数

2.初始化第2个实例(无密码)

3.启动第2个实例

4.未找到实例报错

5.配置第3个实例

6.启动第3个实例

7.停止多实例

8.多实例停不下来处理

9.设置启动关闭服务

10.忘记密码处理

11.安装多版本mysql

11.1安装mysql5.7.32

11.2安装mysql8.0.35


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

  1. [root@localhost ~]# cat /etc/my.cnf
  2. [mysql]
  3. prompt=(\\u@\\h) [\\d]>\\_
  4. [mysqld]
  5. port =3306
  6. user = mysql
  7. datadir = /mysql_data
  8. log_error = error.log
  9. [mysqld_multi]
  10. mysqld = /usr/local/mysql/bin/mysqld_safe
  11. mysqladmin = /usr/local/mysql/bin/mysqladmin
  12. log = /usr/local/mysql/mysqld_multi.log
  13. user = root
  14. pass = 123456
  15. [mysqld1]
  16. port = 3307
  17. datadir = /mysql1_data
  18. 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

  1. [root@localhost ~]# mysqld_multi start 1
  2. [root@localhost ~]# mysqld_multi report
  3. Reporting MySQL servers
  4. MySQL server from group: mysqld1 is running

查看端口

  1. [root@localhost ~]# netstat -ntl
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
  6. tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
  7. tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
  8. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
  9. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
  10. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
  11. tcp6 0 0 ::1:631 :::* LISTEN
  12. tcp6 0 0 ::1:25 :::* LISTEN
  13. tcp6 0 0 ::1:6010 :::* LISTEN
  14. tcp6 0 0 ::1:6011 :::* LISTEN
  15. tcp6 0 0 :::3306 :::* LISTEN
  16. tcp6 0 0 :::3307 :::* LISTEN
  17. tcp6 0 0 :::111 :::* LISTEN
  18. tcp6 0 0 :::22 :::* LISTEN

4.未找到实例报错

  1. [root@localhost mysql]# mysqld_multi report
  2. Reporting MySQL servers
  3. No groups to be reported (check your GNRs)

是因为我在配置文件中的参数写错了,[mysqld1]写成[mysql1]了

5.配置第3个实例

  1. [root@localhost ~]# cat /etc/my.cnf
  2. [mysql]
  3. prompt=(\\u@\\h) [\\d]>\\_
  4. [mysqld]
  5. port =3306
  6. user = mysql
  7. datadir = /mysql_data
  8. log_error = error.log
  9. [mysqld_multi]
  10. mysqld = /usr/local/mysql/bin/mysqld_safe
  11. mysqladmin = /usr/local/mysql/bin/mysqladmin
  12. log = /usr/local/mysql/mysqld_multi.log
  13. [mysqld1]
  14. port = 3307
  15. datadir = /mysql1_data
  16. socket = /tmp/mysql.sock1
  17. [mysqld2]
  18. port = 3308
  19. datadir = /mysql2_data
  20. socket = /tmp/mysql.sock2

6.启动第3个实例

  1. [root@localhost ~]# mysqld_multi start 2
  2. [root@localhost ~]# mysqld_multi report
  3. Reporting MySQL servers
  4. MySQL server from group: mysqld1 is running
  5. MySQL server from group: mysqld2 is running

查看第3个示例端口

  1. [root@localhost ~]# netstat -ntl
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
  6. tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
  7. tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
  8. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
  9. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
  10. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
  11. tcp6 0 0 ::1:631 :::* LISTEN
  12. tcp6 0 0 ::1:25 :::* LISTEN
  13. tcp6 0 0 ::1:6010 :::* LISTEN
  14. tcp6 0 0 ::1:6011 :::* LISTEN
  15. tcp6 0 0 :::3306 :::* LISTEN
  16. tcp6 0 0 :::3307 :::* LISTEN
  17. tcp6 0 0 :::3308 :::* LISTEN
  18. tcp6 0 0 :::111 :::* LISTEN
  19. tcp6 0 0 :::22 :::* LISTEN

7.停止多实例

  1. [root@localhost ~]# mysqld_multi report
  2. Reporting MySQL servers
  3. MySQL server from group: mysqld1 is running
  4. MySQL server from group: mysqld2 is running
  5. [root@localhost mysql1_data]# mysqld_multi stop
  6. [root@localhost mysql1_data]# mysqld_multi report
  7. Reporting MySQL servers
  8. MySQL server from group: mysqld1 is not running
  9. 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.设置启动关闭服务

  1. [root@localhost support-files]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/
  2. [root@localhost support-files]# /etc/init.d/mysqld_multi.server stop
  3. [root@localhost support-files]# mysqld_multi report
  4. Reporting MySQL servers
  5. MySQL server from group: mysqld1 is not running
  6. MySQL server from group: mysqld2 is not running
  7. [root@localhost support-files]# mysqld_multi start
  8. [root@localhost support-files]# mysqld_multi report
  9. Reporting MySQL servers
  10. MySQL server from group: mysqld1 is running
  11. MySQL server from group: mysqld2 is running

mysql是通过mysqld程序来启动的,启动进入mysql后,可以通过shutdown命令关闭

  1. [root@localhost mysql1_data]# mysqld_multi report
  2. Reporting MySQL servers
  3. MySQL server from group: mysqld1 is running
  4. MySQL server from group: mysqld2 is running
  5. [root@localhost mysql1_data]# mysql -S/tmp/mysql.sock1 -p123456
  6. mysql: [Warning] Using a password on the command line interface can be insecure.
  7. Welcome to the MySQL monitor. Commands end with ; or \g.
  8. Your MySQL connection id is 3
  9. Server version: 5.7.44 MySQL Community Server (GPL)
  10. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  11. Oracle is a registered trademark of Oracle Corporation and/or its
  12. affiliates. Other names may be trademarks of their respective
  13. owners.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. (root@localhost) [(none)]> shutdown
  16. -> ;
  17. Query OK, 0 rows affected (0.00 sec)
  18. (root@localhost) [(none)]> exit
  19. Bye
  20. [root@localhost mysql1_data]# mysqld_multi report
  21. Reporting MySQL servers
  22. MySQL server from group: mysqld1 is not running
  23. MySQL server from group: mysqld2 is running
  24. [root@localhost mysql1_data]# mysqladmin -S/tmp/mysql.sock2 -uroot -p123456 shutdown
  25. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  26. [root@localhost mysql1_data]# mysqld_multi report
  27. Reporting MySQL servers
  28. MySQL server from group: mysqld1 is not running
  29. MySQL server from group: mysqld2 is not running

mysql_safe是mysql守护进程,检测mysql进程是否存在,不存在就启动mysql进程

  1. [root@localhost mysql1_data]# ps -ef | grep mysql
  2. 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
  3. 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
  4. root 10494 2407 0 13:33 pts/0 00:00:00 grep --color=auto mysql
  5. [root@localhost mysql1_data]# kill -9 1468
  6. [root@localhost mysql1_data]# ps -ef | grep mysql
  7. 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
  8. 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
  9. 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字段

  1. [root@localhost mysql1_data]# /etc/init.d/mysql.server stop
  2. Shutting down MySQL.. SUCCESS!
  3. [root@localhost mysql1_data]# /etc/init.d/mysql.server start
  4. Starting MySQL. SUCCESS!
  5. [root@localhost mysql1_data]# mysql
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 2
  8. Server version: 5.7.44 MySQL Community Server (GPL)
  9. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  10. Oracle is a registered trademark of Oracle Corporation and/or its
  11. affiliates. Other names may be trademarks of their respective
  12. owners.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. (root@localhost) [(none)]> set password = '123456';
  15. ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  16. (root@localhost) [(none)]> use mysql
  17. Reading table information for completion of table and column names
  18. You can turn off this feature to get a quicker startup with -A
  19. Database changed
  20. (root@localhost) [mysql]> select Host,User,authentication_string from user;
  21. +-----------+---------------+-------------------------------------------+
  22. | Host | User | authentication_string |
  23. +-----------+---------------+-------------------------------------------+
  24. | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
  25. | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  26. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  27. | % | zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
  28. | % | ame | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
  29. | % | lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
  30. +-----------+---------------+-------------------------------------------+
  31. (root@localhost) [mysql]> update user set authentication_string=password('1234567') where User='root' and Host = 'localhost';
  32. Query OK, 1 row affected, 1 warning (0.01 sec)
  33. Rows matched: 1 Changed: 1 Warnings: 1
  34. (root@localhost) [mysql]> flush privileges;
  35. Query OK, 0 row affected (0.00 sec)

另一个窗口,my.cnf中屏蔽掉skip_grant_tables后重启mysql,密码修改成功

  1. [root@localhost support-files]# /etc/init.d/mysql.server restart
  2. Shutting down MySQL.... SUCCESS!
  3. Starting MySQL. SUCCESS!
  4. [root@localhost support-files]# mysql
  5. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  6. [root@localhost support-files]# mysql -uroot -p123456
  7. mysql: [Warning] Using a password on the command line interface can be insecure.
  8. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  9. [root@localhost support-files]# mysql -uroot -p1234567
  10. mysql: [Warning] Using a password on the command line interface can be insecure.
  11. Welcome to the MySQL monitor. Commands end with ; or \g.
  12. Your MySQL connection id is 4
  13. Server version: 5.7.44 MySQL Community Server (GPL)
  14. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  15. Oracle is a registered trademark of Oracle Corporation and/or its
  16. affiliates. Other names may be trademarks of their respective
  17. owners.
  18. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  19. (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
  1. [root@localhost local]# mkdir /mysql32_data
  2. [root@localhost local]# chown mysql:mysql /mysql32_data
  3. [root@localhost local]# ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql32
  4. [root@localhost local]# cat /etc/my.cnf
  5. [mysqld32]
  6. server_id = 32
  7. port = 3332
  8. basedir = /usr/local/mysql32
  9. datadir = /mysql32_data
  10. socket = /tmp/mysql.sock32
  11. [root@localhost mysql32]# bin/mysqld --initialize-insecure --datadir=/mysql32_data
  12. [root@localhost mysql32]# mysqld_multi report
  13. Reporting MySQL servers
  14. MySQL server from group: mysqld32 is not running
  15. MySQL server from group: mysqld1 is running
  16. MySQL server from group: mysqld2 is running
  17. [root@localhost mysql32]# mysqld_multi start 32
  18. [root@localhost mysql32]# mysqld_multi report
  19. Reporting MySQL servers
  20. MySQL server from group: mysqld32 is running
  21. MySQL server from group: mysqld1 is running
  22. MySQL server from group: mysqld2 is running
  23. [root@localhost mysql32_data]# ps -ef | grep mysql
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  1. [root@localhost local]# tar xf mysql-8.0.35-linux-glibc2.12-x86_64.tar.xz
  2. [root@localhost local]# mkdir /mysql80_data
  3. [root@localhost local]# chown mysql:mysql /mysql80_data
  4. [root@localhost local]# ln -s /usr/local/mysql-8.0.35-linux-glibc2.12-x86_64 mysql80
  5. [root@localhost local]# cat /etc/my.cnf
  6. [mysqld80]
  7. port = 3380
  8. basedir = /usr/local/mysql80
  9. datadir = /mysql80_data
  10. socket = /tmp/mysql.sock80
  11. [root@localhost local]# cd mysql80
  12. [root@localhost mysql80]# bin/mysqld --initialize-insecure --datadir=/mysql80_data
  13. [root@localhost mysql80]# mysqld_multi report
  14. Reporting MySQL servers
  15. MySQL server from group: mysqld32 is running
  16. MySQL server from group: mysqld1 is running
  17. MySQL server from group: mysqld2 is running
  18. MySQL server from group: mysqld80 is not running
  19. [root@localhost mysql80]# mysqld_multi start 80
  20. [root@localhost mysql80]# mysqld_multi report
  21. Reporting MySQL servers
  22. MySQL server from group: mysqld32 is running
  23. MySQL server from group: mysqld1 is running
  24. MySQL server from group: mysqld2 is running
  25. MySQL server from group: mysqld80 is running
  26. [root@localhost mysql80]# netstat -nlt
  27. Active Internet connections (only servers)
  28. Proto Recv-Q Send-Q Local Address Foreign Address State
  29. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
  30. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
  31. tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
  32. tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
  33. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
  34. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
  35. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
  36. tcp6 0 0 ::1:631 :::* LISTEN
  37. tcp6 0 0 ::1:25 :::* LISTEN
  38. tcp6 0 0 ::1:6010 :::* LISTEN
  39. tcp6 0 0 ::1:6011 :::* LISTEN
  40. tcp6 0 0 :::33060 :::* LISTEN
  41. tcp6 0 0 :::3332 :::* LISTEN
  42. tcp6 0 0 :::3306 :::* LISTEN
  43. tcp6 0 0 :::3307 :::* LISTEN
  44. tcp6 0 0 :::3308 :::* LISTEN
  45. tcp6 0 0 :::111 :::* LISTEN
  46. tcp6 0 0 :::3380 :::* LISTEN
  47. tcp6 0 0 :::22 :::* LISTEN

需要注意在mysql5.7.4-5.7.10中default_password_lifetime参数默认是360天密码过期,5.7.10后默认密码期限为不过期

  1. [root@localhost mysql80]# mysql -uroot -p123456
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12
  5. Server version: 5.7.44 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. (root@localhost) [(none)]> show variables like 'default_password_lifetime';
  12. +---------------------------+-------+
  13. | Variable_name | Value |
  14. +---------------------------+-------+
  15. | default_password_lifetime | 0 |
  16. +---------------------------+-------+
  17. 1 row in set (0.01 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/279850
推荐阅读
  

闽ICP备14008679号