文中使用mysql5.7 版本實現多實例,端口爲3306和3307。mysql
一、多實例本質
在一臺機器上開啓多個不一樣的mysql服務端口(3306,3307),運行多個mysql服務進程,這些服務進程經過不一樣的socket監聽不一樣的服務端口來提供各自的服務;linux
多個實例共用一套mysql安裝程序,配置文件能夠用同一個(可是最好不一樣,文中用不一樣配置文件),啓動程序能夠用同一個(最好不一樣,文本用不一樣啓動腳本),數據文件是不一樣的(必須不一樣);sql
服務器的硬件資源是公用的,邏輯上多實例是各自獨立的;數據庫
二、多實例做用
有效利用服務器資源;節約服務器資源;
可是多實例確定會存在資源互相搶佔問題,當某個服務實例併發很高或者有慢查詢時,會消耗整臺服務器更多的內存、CPU等,勢必致使其餘實例運行也很慢。vim
三、多實例應用場景
資金比較緊張的公司;
併發訪問不是特別大的業務;bash
四、安裝多實例數據庫服務器
1.建立相應的目錄併發
mkdir /mysql/{3306,3307}/{data,logs,conf,tmp} -p
// 目錄解釋
data # 存放數據 logs # 存放mysql日誌以及binlog日誌 conf # 存放mysql配置文件 tmp # 存放mysql socket文件
2.建立mysql用戶,登陸方式nologin,不建立家目錄app
groupadd mysql -g 1002
useradd mysql -u 1002 -g 1002 -s /sbin/nologin -M
3.安裝一些依賴包socket
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變量進行替換便可