个人公众号

centos 安装多实例数据库

在Centos下安装多个MySql 5.7
① 下载MySql 解压版安装包
② 编写安装脚本
③ 将脚本和安装包放置同一目录
④ 编写my.cnf文件并放置在/etc/ 目录下
⑤ 赋予脚本运行权限并运行
⑥ 修改默认密码
① 下载MySql 解压版安装包
点击传送门:MySql下载 下载对应系统的MySql, Centos选 Linux - Generic <64位> (本人Centos是64位,32位的请选择32位)

 

 


② 编写安装脚本

#!/bin/bash

baseMysqlDir=/usr/local/tarsPkg/mysql-5.7.22-linux-glibc2.12-x86_64

yum install mysql-devel

## 创建 MySQL 软链接
ln -s ${baseMysqlDir} /usr/local/mysql

##创建 MySQL 用户
useradd -r -s /sbin/nologin mysql

## 在 MySQL 二进制包目录中创建 mysql-files 目录
mkdir -v /usr/local/mysql/mysql-files

## 创建多实例数据目录
mkdir -vp /data/mysql_data{1..4} 

## 修改 MySQL 二进制包目录的所属用户与所属组
chown root.mysql -R ${baseMysqlDir}

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

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

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

## 添加mysql 到path
echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile

source /etc/profile

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

## 添加脚本执行权限
chmod +x /etc/init.d/mysqld_multi

## 添加进service服务管理
chkconfig --add mysqld_multi

export PATH=/usr/local/mysql/bin:$PATH

## 查个多实例状态
/etc/init.d/mysqld_multi report

 

 

③ 将脚本和安装包放置同一目录

 

 


④ 编写my.cnf文件并放置在/etc/ 目录下

[mysqld_multi]
mysqld    = /usr/local/mysql/bin/mysqld 
mysqladmin = /usr/local/mysql/bin/mysqladmin
log        = /tmp/mysql_multi.log
[mysqld1]
federated
# 设置数据目录 [多实例中一定要不同]
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
max_connections=1000
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
explicit_defaults_for_timestamp=true
tmpdir=/data/mysql_data1
character_set_server = utf8mb4
[mysqld2]
datadir = /data/mysql_data2
socket = /tmp/mysql.sock2
tmpdir=/data/mysql_data2
port = 3307
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
explicit_defaults_for_timestamp=true
lower_case_table_names = 1
[mysqld3]
datadir = /data/mysql_data3
socket = /tmp/mysql.sock3
tmpdir=/data/mysql_data3
port = 3308
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
explicit_defaults_for_timestamp=true
lower_case_table_names = 1 
[mysqld4]
datadir = /data/mysql_data4
socket = /tmp/mysql.sock4
tmpdir=/data/mysql_data4
port = 3309
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
explicit_defaults_for_timestamp=true

 

⑤ 赋予脚本运行权限并运行

 

 


执行结果:

 

 


⑥ 修改默认密码

 

 


记得修改每一个默认密码哦

posted @ 2019-12-18 14:14  张_俊_杰  阅读(494)  评论(0编辑  收藏  举报