mysql多实例部署

打印 上一主题 下一主题

主题 850|帖子 850|积分 2550

目录

1.软件下载
  1. [root@localhost ~]# cd /usr/src/
  2. [root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
  3. --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
  4. Resolving cdn.mysql.com (cdn.mysql.com)... 23.2.84.230
  5. Connecting to cdn.mysql.com (cdn.mysql.com)|23.2.84.230|:443... connected.
  6. HTTP request sent, awaiting response... 200 OK
  7. Length: 674830866 (644M) [application/x-tar-gz]
  8. Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
  9. mysql-5.7.38-linux-glibc2.12-x 100%[==================================================>] 643.57M  3.35MB/s    in 4m 15s  
  10. 2022-07-29 13:32:30 (2.52 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
  11. [root@localhost src]# ls
  12. debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
复制代码
2.配置用户和组并解压二进制程序至/usr/local下
  1. [root@localhost src]# useradd -M -r -s /sbin/nologin mysql
  2. [root@localhost src]# id mysql
  3. uid=995(mysql) gid=992(mysql) groups=992(mysql)
  4. [root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  5. [root@localhost src]# cd
  6. [root@localhost ~]# ls /usr/local/
  7. bin  etc  games  include  lib  lib64  libexec  mysql-5.7.38-linux-glibc2.12-x86_64  sbin  share  src
  8. [root@localhost ~]# cd /usr/local/
  9. [root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
  10. 'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
  11. [root@localhost local]# ll
  12. total 0
  13. drwxr-xr-x. 2 root root   6 Jun 22  2021 bin
  14. drwxr-xr-x. 2 root root   6 Jun 22  2021 etc
  15. drwxr-xr-x. 2 root root   6 Jun 22  2021 games
  16. drwxr-xr-x. 2 root root   6 Jun 22  2021 include
  17. drwxr-xr-x. 2 root root   6 Jun 22  2021 lib
  18. drwxr-xr-x. 3 root root  17 Jul 12 21:45 lib64
  19. drwxr-xr-x. 2 root root   6 Jun 22  2021 libexec
  20. lrwxrwxrwx. 1 root root  35 Jul 29 13:39 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
  21. drwxr-xr-x. 9 root root 129 Jul 29 13:38 mysql-5.7.38-linux-glibc2.12-x86_64
  22. drwxr-xr-x. 2 root root   6 Jun 22  2021 sbin
  23. drwxr-xr-x. 5 root root  49 Jul 12 21:45 share
  24. drwxr-xr-x. 2 root root   6 Jun 22  2021 src
  25. //修改目录/usr/local/mysql的属主属组
  26. [root@localhost local]# chown -R mysql.mysql mysql*
  27. [root@localhost local]# ll /usr/local/mysql -d
  28. lrwxrwxrwx. 1 mysql mysql 35 Jul 29 13:39 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
  29. //配置环境变量
  30. [root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
  31. [root@localhost local]# source /etc/profile.d/mysql.sh
  32. [root@localhost local]# echo $PATH
  33. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
  34. //创建头文件
  35. [root@localhost ~]# cd /usr/local/mysql
  36. [root@localhost mysql]# ls
  37. LICENSE  README  bin  docs  include  lib  man  share  support-files
  38. [root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql                //配置软链接
  39. '/usr/include/mysql' -> '/usr/local/mysql/include/'               
  40. [root@localhost ~]# chown -R mysql.mysql /usr/include/mysql
  41. [root@localhost ~]# ll -d /usr/include/mysql
  42. lrwxrwxrwx. 1 mysql mysql 25 Jul 29 14:37 /usr/include/mysql -> /usr/local/mysql/include/
  43. [root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
  44. /usr/local/mysql/lib/
  45. [root@localhost ~]# ldconfig
  46. [root@localhost ~]# vim /etc/man_db.conf
  47. #MANDATORY_MANPATH                      /usr/src/pvm3/man
  48. #
  49. MANDATORY_MANPATH                       /usr/man
  50. MANDATORY_MANPATH                       /usr/share/man
  51. MANDATORY_MANPATH                       /usr/local/share/man
  52. MANDATORY_MANPATH                       /usr/local/mysql/man                //添加一行
复制代码
3.创建各实例数据存放的目录
  1. [root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
  2. [root@localhost ~]# chown -R mysql.mysql /opt/data/
  3. [root@localhost ~]# ll /opt/data/
  4. total 0
  5. drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3306
  6. drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3307
  7. drwxr-xr-x. 2 mysql mysql 6 Jul 29 13:45 3308
  8. [root@localhost ~]# tree /opt/data/
  9. /opt/data/
  10. |-- 3306
  11. |-- 3307
  12. `-- 3308
复制代码
4.初始化各示例
  1. //初始化3306
  2. [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306
  3. 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).
  4. 2022-07-29T05:48:04.369296Z 0 [Warning] InnoDB: New log files created, LSN=45790
  5. 2022-07-29T05:48:04.403256Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  6. 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.
  7. 2022-07-29T05:48:04.464340Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  8. 2022-07-29T05:48:04.643259Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
  9. 2022-07-29T05:48:04.643284Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
  10. 2022-07-29T05:48:04.643585Z 0 [Warning] CA certificate ca.pem is self signed.
  11. 2022-07-29T05:48:04.717924Z 1 [Note] A temporary password is generated for root@localhost: ,dASzpwwE3RJ
  12. [root@localhost ~]# echo ',dASzpwwE3RJ' > 3306
  13. [root@localhost ~]# ls
  14. 3306  anaconda-ks.cfg
  15. //初始化3307
  16. [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
  17. 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).
  18. 2022-07-29T05:49:29.195218Z 0 [Warning] InnoDB: New log files created, LSN=45790
  19. 2022-07-29T05:49:29.224130Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  20. 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.
  21. 2022-07-29T05:49:29.283175Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  22. 2022-07-29T05:49:29.523082Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
  23. 2022-07-29T05:49:29.523118Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
  24. 2022-07-29T05:49:29.523503Z 0 [Warning] CA certificate ca.pem is self signed.
  25. 2022-07-29T05:49:29.581009Z 1 [Note] A temporary password is generated for root@localhost: )MhldlF<o23j
  26. [root@localhost ~]# echo ')MhldlF<o23j' > 3307
  27. [root@localhost ~]# ls
  28. 3306  3307  anaconda-ks.cfg
  29. //初始化3308
  30. [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
  31. 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).
  32. 2022-07-29T05:50:14.892190Z 0 [Warning] InnoDB: New log files created, LSN=45790
  33. 2022-07-29T05:50:14.915269Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  34. 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.
  35. 2022-07-29T05:50:14.974697Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  36. 2022-07-29T05:50:15.340479Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
  37. 2022-07-29T05:50:15.340510Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
  38. 2022-07-29T05:50:15.341068Z 0 [Warning] CA certificate ca.pem is self signed.
  39. 2022-07-29T05:50:15.368072Z 1 [Note] A temporary password is generated for root@localhost: tkdnhiEk>5vh
  40. [root@localhost ~]# echo 'tkdnhiEk>5vh' > 3308
  41. [root@localhost ~]# ls
  42. 3306  3307  3308  anaconda-ks.cfg
复制代码
5.配置配置文件/etc/my.cnf
  1. [root@localhost ~]# vim /etc/my.cnf
  2. [root@localhost ~]# cat /etc/my.cnf
  3. [mysqld_multi]
  4. mysqld = /usr/local/mysql/bin/mysqld_safe
  5. mysqladmin = /usr/local/mysql/bin/mysqladmin
  6. [mysqld3306]
  7. datadir = /opt/data/3306
  8. port = 3306
  9. socket = /tmp/mysql3306.sock
  10. pid-file = /opt/data/3306/mysql_3306.pid
  11. log-error=/var/log/3306.log
  12. [mysqld3307]
  13. datadir = /opt/data/3307
  14. port = 3307
  15. socket = /tmp/mysql3307.sock
  16. pid-file = /opt/data/3307/mysql_3307.pid
  17. log-error=/var/log/3307.log
  18. [mysqld3308]
  19. datadir = /opt/data/3308
  20. port = 3308
  21. socket = /tmp/mysql3308.sock
  22. pid-file = /opt/data/3308/mysql_3308.pid
  23. log-error=/var/log/3308.log
复制代码
6.启动各示例
  1. [root@localhost ~]# mysqld_multi start 3306
  2. -bash: /usr/local/mysql/bin/mysqld_multi: /usr/bin/perl: bad interpreter: No such file or directory                                //需要下载perl包
  3. [root@localhost ~]# dnf -y install perl
  4. [root@localhost mysql]# yum install ncurses-compat-libs
  5. [root@localhost mysql]# file /usr/local/mysql/bin/mysqld_multi
  6. /usr/local/mysql/bin/mysqld_multi: Perl script text executable
  7. [root@localhost ~]# mysqld_multi start 3306
  8. [root@localhost ~]# mysqld_multi start 3307
  9. [root@localhost ~]# mysqld_multi start 3308
  10. [root@localhost ~]# ss -anlt
  11. State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
  12. LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
  13. LISTEN        0             80                               *:3308                          *:*                          
  14. LISTEN        0             128                           [::]:22                         [::]:*                          
  15. LISTEN        0             80                               *:3306                          *:*                          
  16. LISTEN        0             80                               *:3307                          *:*                          
复制代码
7.使用system控制启动
  1. //需要先把mysql服务停止 //mysqld_mulit 命令停止不了只能杀进程
  2. [root@localhost ~]# ps -ef | grep 3306 | grep -v grep
  3. 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
  4. 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
  5. [root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}'
  6. 77016
  7. 77152
  8. [root@localhost ~]# ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
  9. [root@localhost ~]# ss -anlt
  10. State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
  11. LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
  12. LISTEN        0             80                               *:3308                          *:*                          
  13. LISTEN        0             128                           [::]:22                         [::]:*                          
  14. LISTEN        0             80                               *:3307                          *:*                          
  15. [root@localhost ~]# ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
  16. [root@localhost ~]# ss -anlt
  17. State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
  18. LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
  19. LISTEN        0             80                               *:3308                          *:*                          
  20. LISTEN        0             128                           [::]:22                         [::]:*                          
  21. [root@localhost ~]# ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
  22. [root@localhost ~]# ss -anlt
  23. State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
  24. LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
  25. LISTEN        0             128                           [::]:22                         [::]:*                        
  26. //找个一个系统默认可以使用system控制的配置文件进行cp一下,并进行修改
  27. [root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service
  28. //配置3306
  29. [root@localhost ~]# vim /usr/lib/systemd/system/3306.service
  30. [Unit]
  31. Description=3306 server daemon
  32. After=network.target sshd-keygen.target
  33. [Service]
  34. Type=forking
  35. ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
  36. ExecStop=ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9
  37. ExecReload=/bin/kill -HUP $MAINPID
  38. [Install]
  39. WantedBy=multi-user.target
  40. //配置3307
  41. [root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service
  42. [root@localhost ~]# vim /usr/lib/systemd/system/3307.service    //使用:% s/6/7/g一键替换
  43. [Unit]
  44. Description=3307 server daemon
  45. After=network.target sshd-keygen.target
  46. [Service]
  47. Type=forking
  48. ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307
  49. ExecStop=ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9
  50. ExecReload=/bin/kill -HUP $MAINPID
  51. [Install]
  52. WantedBy=multi-user.target
  53. //配置3308
  54. [root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service
  55. [root@localhost ~]# vim /usr/lib/systemd/system/3308.service     //使用:% s/6/7/g一键替换
  56. [Unit]
  57. Description=3308 server daemon
  58. After=network.target sshd-keygen.target
  59. [Service]
  60. Type=forking
  61. ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308
  62. ExecStop=ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9
  63. ExecReload=/bin/kill -HUP $MAINPID
  64. [Install]
  65. WantedBy=multi-user.target
  66. //刷新一下
  67. [root@localhost ~]# systemctl daemon-reload               
  68. //关闭防火墙和selinux
  69. [root@localhost ~]# systemctl stop firewalld
  70. [root@localhost ~]# systemctl disable firewalld
  71. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
  72. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  73. [root@localhost ~]# setenforce 0
  74. [root@localhost ~]# vim /etc/selinux/config
  75. SELINUX=disabled
  76. //启动3306,3307,3308
  77. [root@localhost ~]# systemctl start 3306
  78. Job for 3306.service failed because the control process exited with error code.
  79. See "systemctl status 3306.service" and "journalctl -xe" for details.  //没有找到my_print_defaults这个文件
  80. //创建一个软链接到/usr/bin下
  81. [root@localhost ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
  82. [root@localhost ~]# systemctl start 3306
  83. [root@localhost ~]# systemctl start 3307
  84. [root@localhost ~]# systemctl start 3308
  85. [root@localhost ~]# ss -anlt
  86. State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
  87. LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
  88. LISTEN        0             80                               *:3308                          *:*                          
  89. LISTEN        0             128                           [::]:22                         [::]:*                          
  90. LISTEN        0             80                               *:3306                          *:*                          
  91. LISTEN        0             80                               *:3307                          *:*                          
复制代码
8.初始化密码
  1. //3306
  2. [root@localhost ~]# mysql -uroot -p',dASzpwwE3RJ' -S /tmp/mysql.sock
  3. mysql> set password = password('123456');
  4. Query OK, 0 rows affected, 1 warning (0.00 sec)
  5. //3307
  6. [root@localhost ~]# mysql -uroot -p')MhldlF<o23j' -S /tmp/mysql3307.sock
  7. mysql> set password = password('123456');
  8. Query OK, 0 rows affected, 1 warning (0.00 sec)
  9. //3308
  10. [root@localhost ~]# mysql -uroot -p'tkdnhiEk>5vh' -S /tmp/mysql3308.sock
  11. mysql> set password = password('123456');
  12. Query OK, 0 rows affected, 1 warning (0.00 sec)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表