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

1. 背景

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

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

2. 多实例特点

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

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

3. 环境 [ 关闭SeLinux ]

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)

[[email protected] ~]# uname -r
2.6.32-504.el6.x86_64

[[email protected] ~]# getenforce 
Disabled

4. MySQL 二进制包准备

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

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

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

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

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

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

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

* 创建 MySQL 用户

[[email protected] ~]# useradd -r -s /sbin/nologin mysql

* 创建MySQL数据目录

[[email protected] ~]# 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 数据目录所属用户与所属组

[[email protected] ~]# chown mysql.mysql -R /data/mysql_data_*

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

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

* MySQL 5.5 初始化

[[email protected] ~]# chown mysql.mysql -R /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64
[[email protected] ~]# /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 修改密码

[[email protected] ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_5 &
[[email protected] ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server 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 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(‘123‘);
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[[email protected] ~]# killall mysqld

* MySQL 5.6 初始化

[[email protected] ~]# chown mysql.mysql -R /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64
[[email protected] ~]# /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修改密码

[[email protected] ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_6 &
[[email protected] ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server 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 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(‘123‘);
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[[email protected] ~]# killall mysqld

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

[[email protected] ~]# mkdir /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files
[[email protected] ~]# chown root.mysql -R /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64
[[email protected] ~]# chown mysql.mysql -R /data/mysql_data_7 /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files
[[email protected] ~]# /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修改密码

[[email protected] ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_7 &
[[email protected] ~]# /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 4
Server version: 5.7.18

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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 = ‘123‘;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[[email protected] ~]# 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

[mysqld_multi]
# 设置multi 日志
log = /tmp/mysql_multi.log

[mysqld5]
# 设置实例所在目录
basedir = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64
# 设置实例数据目录 -- 多实例中一定要不同
datadir = /data/mysql_data_5
# 设置socket 文件路径 -- 多实例中一定要不同
socket = /tmp/mysql.sock5
# 设置实例监听端口 -- 多实例中一定要不同
port = 3305

[mysqld6]
basedir = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64
datadir = /data/mysql_data_6
socket = /tmp/mysql.sock6
port = 3306

[mysqld7]
basedir = /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64
datadir = /data/mysql_data_7
socket = /tmp/mysql.sock7
port = 3307

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

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

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

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

7. 测试

* 查看多实例状态

[[email protected] ~]# /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld5 is not running
MySQL server from group: mysqld6 is not running
MySQL server from group: mysqld7 is not running

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

[[email protected] ~]# /etc/init.d/mysqld_multi start 
[[email protected] ~]# /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld5 is running
MySQL server from group: mysqld6 is running
MySQL server from group: mysqld7 is running
[[email protected] ~]# netstat -lntp | grep mysqld
tcp        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

* 分别连接实例

[[email protected] ~]# mysql -S /tmp/mysql.sock5
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server 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 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> quit
Bye

[[email protected] ~]# mysql -S /tmp/mysql.sock6
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server 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 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> quit
Bye

[[email protected] ~]# mysql -S /tmp/mysql.sock7
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server 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 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> quit
Bye

* 停止多实例

[[email protected] ~]# /etc/init.d/mysqld_multi stop
[[email protected] ~]# /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld5 is not running
MySQL server from group: mysqld6 is not running
MySQL server from group: mysqld7 is not running

8. 总结

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

时间: 2024-08-30 00:24:05

MySQL--------多版本多实例混合部署的相关文章

MySQL多版本多实例的实现

最近有好多的朋友在使用MySQL的时候遇到这样的问题:原本在服务器中装有比较老版本的MySQL,如5.1,5.5等等.应某些业务需要,不得不安装更新的版本,如5.6或5.7或更高. 这样的话,就势必会面临下述诸多的问题: 1.我们到底使用哪个版本的程序来启动MySQL服务? 2.MySQL进程是否会有冲突? 3.如何控制不同版本所监听的套接字? 于是,我便开始了尝试,经过了若干次的失败,最终实现了这样的要求,只是实现的还是比较粗糙和简陋,各实例的配置文件和服务启动脚本之类的还有待于进一步完善.

MySQL运维-多实例部署

MySQL多实例是在同一台数据库服务器上,通过开启多个不同的服务端口,并被对应的socket监听,以实现同时运行多个MySQL服务进程的目的. 实验环境使用<MySQL运维-单实例安装>部署成功后的实验环境mysql实例1安装目录:/data/3306mysql实例1数据文件目录:/data/3306/dbfilemysql实例2安装目录:/data/3307mysql实例2数据文件目录:/data/3307/dbfile 停止MySQL []# service mysqld stopShut

MySQL多实例安装部署

解压 MySQL 5.7 二进制包到指定目录[[email protected] ~]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ 创建 MySQL 软链接[[email protected] ~]#mv /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql 创建 MySQL 用户[[email protected] ~]#groupa

MySQL 5.6 多实例部署

1.安装MySQL # useradd -s /sbin/nologin -M mysql # tar -xvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz -C /usr/local # mv mysql-5.6.41-linux-glibc2.12-x86_64  mysql # chown -R mysql:mysql  /usr/local/mysql 2.配置多实例配置文件目录 # mkdir /data/{3306,3307}/data #

MySQL各版本的区别

文章出自:http://blog.sina.com.cn/s/blog_62b37bfe0101he5t.html 感谢作者的分享 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.2. MySQL Enterprise Edition 企业版本,需付费,可以试用30天.3. MySQL Cluster 集群版,开源免费.可将

mysql系列之多实例2----基于多配置文件

经过上一篇博文mysql系列之多实例1----介绍对mysql多实例进行了简单的介绍,本片博文将开始针对mysql多实例的第一种实现方案,基于多配置文件的mysql多实例进行部署实现. 环境: CentOS 6.5 x86_64位 采用最小化安装,系统经过了基本优化 selinux 为关闭状态,iptables 为无限制模式 mysql版本:mysql-5.5.38 源码包存放位置:/usr/local/src 源码包编译安装位置:/usr/local/mysql 数据库存放位置:/mydata

mysql各版本区别

MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持. 2. MySQL Enterprise Edition 企业版本,需付费,可以试用30天. 3. MySQL Cluster 集群版,开源免费.可将几个MySQL Server封装成一个Server. 4. MySQL Cluster CGE 高级集群版,需付费. 5. My

【Database】MySQL各版本的区别

MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.2. MySQL Enterprise Edition 企业版本,需付费,可以试用30天.3. MySQL Cluster 集群版,开源免费.可将几个MySQL Server封装成一个Server.4. MySQL Cluster CGE 高级集群版,需付费.5. MySQL

MySQL编译安装多实例

MySQL数据库多实例安装 第1章 MySQL多实例介绍 简单的说,MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306.3307),同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务. 这些MySQL多实例共用一套MySQL安装程序,使不同的my.cnf(也可以相同)配置文件.启动程序(也可以相同)和数据文件.在提供服务时,多实例MySQL在逻辑上看来是各自独立的,它们根据配置文件的对应设定值,获得服务器相应数量的硬件资源. 打