MySQL5.7多实例

database

文中使用mysql5.7 版本实现多实例,端口为3306和3307。

1、多实例本质
在一台机器上开启多个不同的mysql服务端口(3306,3307),运行多个mysql服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供各自的服务;

多个实例共用一套mysql安装程序,配置文件可以用同一个(但是最好不同,文中用不同配置文件),启动程序可以用同一个(最好不同,文本用不同启动脚本),数据文件是不同的(必须不同);

服务器的硬件资源是公用的,逻辑上多实例是各自独立的;

2、多实例作用
有效利用服务器资源;节约服务器资源;
但是多实例肯定会存在资源互相抢占问题,当某个服务实例并发很高或者有慢查询时,会消耗整台服务器更多的内存、CPU等,势必导致其他实例运行也很慢。

3、多实例应用场景
资金比较紧张的公司;
并发访问不是特别大的业务;

4、安装多实例数据库

1.创建相应的目录

mkdir /mysql/{3306,3307}/{data,logs,conf,tmp} -p

// 目录解释

data # 存放数据

logs # 存放mysql日志以及binlog日志

conf # 存放mysql配置文件

tmp # 存放mysql socket文件

2.创建mysql用户,登录方式nologin,不创建家目录

groupadd mysql -g 1002

useradd mysql -u 1002 -g 1002 -s /sbin/nologin -M

3.安装一些依赖包

yum install ncurses-devel libaio-devel -y

4.上传mysql 5.7至本机/application目录

// 将mysql5.7 解压并移动到/application/mysql-5.7.20 目录下

mkdir -p /application/mysql-5.7.20

tar xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz ./mysql-5.7.20

mv mysql-5.7.20-linux-glibc2.12-x86_64/* mysql-5.7.20/

rm -rf mysql-5.7.20-linux-glibc2.12-x86_64

// 软连接

ln -s /application/mysql-5.7.20/ /application/mysql

// 将 /application/mysql/bin 目录加入PATH环境变量

vim /etc/profile

export MYSQL_HOME=/application/mysql/bin

export PATH=$PATH:$MYSQL_HOME

5.每个实例配置my.cnf文件

3306实例

# vim /mysql/3306/conf/my.cnf

[mysqld]

basedir=/application/mysql

datadir=/mysql/3306/data

tmpdir = /mysql/3306/tmp

socket=/mysql/3306/tmp/mysql.sock

log-error=/mysql/3306/logs/mysql.log

port=3306

server_id=3306

max_connections = 1000

innodb_buffer_pool_size = 260M

skip_name_resolve

# 字符集

character-set-server = utf8mb4

collation-server = utf8mb4_general_ci

nit_connect="SET NAMES utf8mb4"

# bin-log日志

log-bin=/mysql/3306/data/mysql-bin

binlog_format=row

# 慢日志

slow_query_log = 1

slow_query_log_file = /mysql/3306/logs/slow.log

log_queries_not_using_indexes = 1

log_throttle_queries_not_using_indexes = 5

long_query_time = 1

[mysql]

socket=/mysql/3306/tmp/mysql.sock

[mysqladmin]

socket=/mysql/3306/tmp/mysql.sock

3307实例

# vim /mysql/3307/conf/my.cnf

[mysqld]

basedir=/application/mysql

datadir=/mysql/3307/data

tmpdir = /mysql/3307/tmp

socket=/mysql/3307/tmp/mysql.sock

log-error=/mysql/3307/logs/mysql.log

port=3307

server_id=3307

max_connections = 1000

innodb_buffer_pool_size = 260M

skip_name_resolve

# 字符集

character-set-server = utf8mb4

collation-server = utf8mb4_general_ci

# bin-log日志

log-bin=/mysql/3307/data/mysql-bin

binlog_format=row

# 慢日志

slow_query_log = 1

slow_query_log_file = /mysql/3307/logs/slow.log

log_queries_not_using_indexes = 1

log_throttle_queries_not_using_indexes = 5

long_query_time = 1

[mysql]

socket=/mysql/3307/tmp/mysql.sock

[mysqladmin]

socket=/mysql/3307/tmp/mysql.sock

6.使用mysqld命令初始化mysql数据库文件

// 授权mysql用户管理相应的mysql目录

chown -R mysql:mysql /mysql/

// 初始化多实例

mysqld --defaults-file=/mysql/3306/conf/my.cnf --basedir=/application/mysql --initialize-insecure --user=mysql

mysqld --defaults-file=/mysql/3307/conf/my.cnf --basedir=/application/mysql --initialize-insecure --user=mysql

7.通过mysqld_safe命令启动多实例mysql

mysqld_safe --defaults-file=/mysql/3306/conf/my.cnf &

mysqld_safe --defaults-file=/mysql/3307/conf/my.cnf &

8.检查操作

// 查看进程是否存在

ps -ef | grep mysql | grep -v "grep"

// 查看端口是否存在

ss -lntup | grep -E "3306|3307"

// 本地登录(-S 指定不同实例的socket)

mysql -uroot -p -S /mysql/3306/tmp/mysql.sock

9.多实例启动脚本

#!/bin/bash

PORT=3306

SOCK_FILE_LOCK=/mysql/${PORT}/tmp/mysql.sock.lock

# start

function func_start(){

if [ -f ${SOCK_FILE_LOCK} ];then

echo "MySQL ${PORT} has started..."

exit 1

else

echo "MySQL ${PORT} is start..."

mysqld_safe --defaults-file=/mysql/3306/conf/my.cnf >/dev/null &

result=$?

[ ${result} -ne 0 ] && echo "MySQL ${PORT} start failed..." || echo "MySQL ${PORT} start success.."

fi

}

# stop

function func_stop(){

if [ -f ${SOCK_FILE_LOCK} ];then

echo "MySQL ${PORT} is stop..."

kill $(cat /mysql/${PORT}/tmp/mysql.sock.lock)

count=0

while [ $count -ne 60 ]

do

((count++))

# echo $count

sleep 1

if [ ! -f ${SOCK_FILE_LOCK} ];then

echo "MySQL ${PORT} stop success..."

break

else

echo "MySQL ${PORT} is stop..."

fi

done

else

echo "MySQL ${PORT} don"t start..."

exit 1

fi

}

case $1 in

start)

func_start;;

stop)

func_stop;;

restart)

func_stop

sleep 3

func_start

echo "MySQL ${PORT} restart success...";;

esac

只需要将PORT变量进行替换即可

以上是 MySQL5.7多实例 的全部内容, 来源链接: utcz.com/z/531678.html

回到顶部