文中使用mysql5.7 版本實現多實例,埠為3306和3307。 1、多實例本質在一臺機器上開啟多個不同的mysql服務埠(3306,3307),運行多個mysql服務進程,這些服務進程通過不同的socket監聽不同的服務埠來提供各自的服務; 多個實例共用一套mysql安裝程式,配置文件可以 ...
文中使用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變數進行替換即可