MySQL多实例安装部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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] ~]#groupadd -r mysql
    [[email protected] ~]# useradd -r -g mysql -s /bin/false -M mysql

  • 在 MySQL 二进制包目录中创建 mysql-files 目录 [MySQL 数据导入/导出数据专放目录]
    [[email protected] ~]# mkdir -v /usr/local/mysql/mysql-files
    mkdir: created directory `/usr/local/mysql/mysql-files‘

  • 创建多实例数据目录
    [[email protected] ~]# mkdir -vp /data/mysql_data{1..4}
    mkdir: created directory /data‘ <br/>mkdir: created directory/data/mysql_data1‘
    mkdir: created directory /data/mysql_data2‘ <br/>mkdir: created directory/data/mysql_data3‘
    mkdir: created directory `/data/mysql_data4‘

  • 修改 MySQL 二进制包目录的所属用户与所属组
    1 [[email protected] ~]# chown root.mysql -R /usr/local/mysql

  • 修改 MySQL 多实例数据目录与 数据导入/导出专放目录的所属用户与所属组
    [[email protected] ~]# chown mysql.mysql -R /usr/local/mysql/mysql-files /data/mysql_data{1..4}

  • 配置 MySQL 配置文件 /etc/my.cnf
    [mysqld_multi]
    mysqld = /usr/local/mysql/bin/mysqld
    mysqladmin = /usr/local/mysql/bin/mysqladmin
    log = /tmp/mysql_multi.log

[mysqld1]
# 设置数据目录 [多实例中一定要不同]
datadir = /data/mysql_data1
# 设置sock存放文件名 [多实例中一定要不同]
socket = /tmp/mysql.sock1
# 设置监听开放端口 [多实例中一定要不同]
port = 3306
# 设置运行用户
user = mysql
# 关闭监控
performance_schema = off
# 设置innodb 缓存大小
innodb_buffer_pool_size = 32M
# 设置监听IP地址
bind_address = 0.0.0.0
# 关闭DNS 反向解析
skip-name-resolve = 0 **

[mysqld2]
datadir = /data/mysql_data2
socket = /tmp/mysql.sock2
port = 3307
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0

[mysqld3]
datadir = /data/mysql_data3
socket = /tmp/mysql.sock3
port = 3308
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0

[mysqld4]
datadir = /data/mysql_data4
socket = /tmp/mysql.sock4
port = 3309
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0

  • 初始化各个实例 [ 初始化完成后会自带随机密码在输出日志中 ]
    [[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1
    [[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2
    [[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3
    [[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4

  • 各实例开启 SSL 连接
    [[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1
    [[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2
    [[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3
    [[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4

*修改环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

  • 复制多实例脚本到服务管理目录下 [ /etc/init.d/ ]
    [[email protected] ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi

  • 添加脚本执行权限
    [[email protected] ~]# chmod +x /etc/init.d/mysqld_multi

  • 添加进service服务管理
    [[email protected] ~]# chkconfig --add mysqld_multi
  1. 启动测试
    • 查个多实例状态
      [[email protected] ~]# /etc/init.d/mysqld_multi report
      Reporting MySQL servers
      MySQL server from group: mysqld1 is not running
      MySQL server from group: mysqld2 is not running
      MySQL server from group: mysqld3 is not running
      MySQL server from group: mysqld4 is not running
  • 启动多实例
    [[email protected] ~]# /etc/init.d/mysqld_multi start

  • 查看多实例状态
    Reporting MySQL servers
    MySQL server from group: mysqld1 is running
    MySQL server from group: mysqld2 is running
    MySQL server from group: mysqld3 is running
    MySQL server from group: mysqld4 is running

  • 查看实例监听端口
    [[email protected] ~]# netstat -lntp | grep mysqld
    tcp 0 0 0.0.0.0:3306 0.0.0.0: LISTEN 2673/mysqld
    tcp 0 0 0.0.0.0:3307 0.0.0.0:
    LISTEN 2676/mysqld
    tcp 0 0 0.0.0.0:3308 0.0.0.0: LISTEN 2679/mysqld
    tcp 0 0 0.0.0.0:3309 0.0.0.0:
    LISTEN 2682/mysqld
  1. 连接测试

以上是关于MySQL多实例安装部署的主要内容,如果未能解决你的问题,请参考以下文章

mysql-5.6.36单/多实例部署

MySQL多实例安装部署

linux12 -MYSQL数据库 -->02数据库安装多实例部署

MySQL5.6多实例部署

MYSQL的单机多实例部署

MySQL5.6多实例部署