千家信息网

MySQL--------多版本多实例混合部署

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,1. 背景* MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的dat
千家信息网最后更新 2025年02月03日MySQL--------多版本多实例混合部署

1. 背景

* MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的datadir, port, socket, pid都是不同的。

* 网上多实例一般通过实例版本相同实现,此次以不同版本来实现多实例部署(5.5、5.6、5.7)。


2. 多实例特点

* 有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。

* 资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降。


3. 环境 [ 关闭SeLinux ]

[root@MySQL ~]# cat /etc/redhat-release CentOS release 6.9 (Final)[root@MySQL ~]# uname -r2.6.32-504.el6.x86_64[root@MySQL ~]# getenforce Disabled


4. MySQL 二进制包准备

* 下载官方5.5二进制安装包

[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.57-linux-glibc2.12-x86_64.tar.gz

* 下载官方5.6二进制安装包

[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz

* 下载官方5.7二进制安装包

[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar


5. mysql 版本初始化并统一修改密码

* 创建 MySQL 用户

[root@MySQL ~]# useradd -r -s /sbin/nologin mysql


* 创建MySQL数据目录

[root@MySQL ~]# mkdir -vp /data/mysql_data_{5..7}mkdir: created directory `/data'mkdir: created directory `/data/mysql_data_5'mkdir: created directory `/data/mysql_data_6'mkdir: created directory `/data/mysql_data_7'


* 修改MySQL 数据目录所属用户与所属组

[root@MySQL ~]# chown mysql.mysql -R /data/mysql_data_*


* 解压MySQL 各版本至 /usr/local 目录

[root@MySQL ~]# tar zxf mysql-5.5.57-linux-glibc2.12-x86_64.tar.gz -C /usr/local/[root@MySQL ~]# tar zxf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/[root@MySQL ~]# tar xf mysql-5.7.19-linux-glibc2.12-x86_64.tar -C /usr/local/


* MySQL 5.5 初始化

[root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64[root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/scripts/mysql_install_db --user=mysql --datadir=/data/mysql_data_5 --basedir=/usr/local/mysql-5.5.57-linux-glibc2.12-x86_64

* MySQL 5.5 修改密码

[root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_5 &[root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.57 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set password = password('123');Query OK, 0 rows affected (0.00 sec)mysql> quitBye[root@MySQL ~]# killall mysqld


* MySQL 5.6 初始化

[root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64[root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/scripts/mysql_install_db --user=mysql --datadir=/data/mysql_data_6 --basedir=/usr/local/mysql-5.6.37-linux-glibc2.12-x86_64

* MySQL 5.6修改密码

[root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_6 &[root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.37 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set password = password('123');Query OK, 0 rows affected (0.00 sec)mysql> quitBye[root@MySQL ~]# killall mysqld


* MySQL 5.7 初始化 [ 注意初始化提示的随机密码 ]

[root@MySQL ~]# mkdir /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files[root@MySQL ~]# chown root.mysql -R /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64[root@MySQL ~]# chown mysql.mysql -R /data/mysql_data_7 /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files[root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --datadir=/data/mysql_data_7 --basedir=/usr/local/mysql-5.7.19-linux-glibc2.12-x86_64


* MySQL 5.7修改密码

[root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_7 &[root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysql -p'INoGk(hoj9>/'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 4Server version: 5.7.18Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set password = '123';Query OK, 0 rows affected (0.00 sec)mysql> quitBye[root@MySQL ~]# killall mysqld


6. 多版本部署

* 编辑/etc/my.cnf

[client]# 设置登陆用户user = root# 设置登陆用户密码password = 123[mysqld]# mysql 运行用户user = mysql# 设置 mysql 监听 IP 地址bind_address = 0.0.0.0# 关闭 DNS 反解析skip-name-resolve = 0# 关闭监听performance_schema = off# 设置buffer pool 大小innodb_buffer_pool_size = 32M# 设置错误日志文件名log_error = error.log[mysqld_multi]# 设置multi 日志log = /tmp/mysql_multi.log[mysqld5]# 设置实例所在目录basedir = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64# 设置mysql 运行程序所在路径mysqld = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld# 设置mysql 管理运行程序所在路径mysqladmin = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqladmin# 设置实例数据目录 -- 多实例中一定要不同datadir = /data/mysql_data_5# 设置socket 文件路径 -- 多实例中一定要不同socket = /tmp/mysql.sock5# 设置实例监听端口 -- 多实例中一定要不同port = 3305[mysqld6]basedir = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64mysqld = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqldmysqladmin = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqladmindatadir = /data/mysql_data_6socket = /tmp/mysql.sock6port = 3306[mysqld7]basedir = /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64datadir = /data/mysql_data_7socket = /tmp/mysql.sock7port = 3307


* 从随意版本二进制包中support-files目录下复制mysqld_multi.server启动脚本至 /etc/init.d/

[root@MySQL ~]# cp /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/support-files/mysqld_multi.server /etc/init.d/mysqld_multi[root@MySQL ~]# chmod +x /etc/init.d/mysqld_multi


* 随意版本创始软链接,并设置环境变量

[root@MySQL ~]# ln -s /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql[root@MySQL ~]# export PATH=/usr/local/mysql/bin:$PATH

7. 测试

* 查看多实例状态

[root@MySQL ~]# /etc/init.d/mysqld_multi reportReporting MySQL serversMySQL server from group: mysqld5 is not runningMySQL server from group: mysqld6 is not runningMySQL server from group: mysqld7 is not running


* 启动多实例 [ 需等候几秒 ]

[root@MySQL ~]# /etc/init.d/mysqld_multi start [root@MySQL ~]# /etc/init.d/mysqld_multi reportReporting MySQL serversMySQL server from group: mysqld5 is runningMySQL server from group: mysqld6 is runningMySQL server from group: mysqld7 is running[root@MySQL ~]# netstat -lntp | grep mysqldtcp        0      0 0.0.0.0:3305                0.0.0.0:*                   LISTEN      43750/mysqld        tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      43753/mysqld        tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      43756/mysqld


* 分别连接实例

[root@MySQL ~]# mysql -S /tmp/mysql.sock5Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.18 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quitBye[root@MySQL ~]# mysql -S /tmp/mysql.sock6Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.18 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quitBye[root@MySQL ~]# mysql -S /tmp/mysql.sock7Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.18 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quitBye


* 停止多实例

[root@MySQL ~]# /etc/init.d/mysqld_multi stop[root@MySQL ~]# /etc/init.d/mysqld_multi reportReporting MySQL serversMySQL server from group: mysqld5 is not runningMySQL server from group: mysqld6 is not runningMySQL server from group: mysqld7 is not running


8. 总结


以需求驱动技术,技术本身没有优略之分,只有业务之分。

实例 服务 版本 不同 密码 目录 二进制 用户 资源 数据 服务器 监听 运行 官方 所在 路径 所属 技术 文件 日志 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全环境未能通过验证视频 java软件开发招聘信息 有不用服务器的联网游戏吗 印度多IP服务器租用 三一搅拌站数据库密码忘了怎么办 什么是业务数据库 2022网络安全法实施 四级安全生产事故数据库 天津极睿软件开发有限公司 南方网通网络技术 市北区平台软件开发外包公司 香港汇丰银行网络技术覃启华 宝塔服务器多个ip多站 惟旅网络技术上海有限公司 烟台公司景明网络技术支持 零基础自学软件开发网站 本地电脑建立服务器的命令 北京微软网络技术公司的负责人 管理数据库的web应用 传奇世界单职业服务器 短信平台数据库设计 供应优惠的聊天软件开发 网络安全在行动的心得体会 数据库表web页面 软件开发哪个方向好 r软件中怎样提取行数据库 服务器上市时间表 现在软件开发用什么软件 落实网络安全责任制度 普陀区数据网络技术服务有哪些
0