mysql多实例
部署多实例
基于一个mysql应用
初始化三次,生成三个独立的mysql数据库目录,即为三个mysql独立的实例
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
二进制方式安装mysql
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
安装mysql运行所需要的基础依赖
yum install ncurse-devel libaio-devel gcc make cmake -y
环境的清理
清空PATH有关的mysql 注释掉
停止当前linux的mysql,(如果存在mysql端口的话)
创建mysql用户,前面创建过,无需创建
准备好mysql多实例的数据目录
[root@160-116-208-21 ~]# mkdir -p/my_mysql/{3306,3307}
[root@160-116-208-21 ~]# tree /my_mysql/
/my_mysql/
├── 3306
└── 3307
解压缩二进制的mysql软件包
tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz -C /application/ #-C 指定目录解压缩
准备二进制mysql运行所需的环境
准备多个实例的配置文件
vim /my_mysql/3306/my.cnf
3306
[client]
[mysqld]
port=3306
socket=/my_mysql/3306/mysql.sock
basedir=/application/mysql-5.6.43-linux-glibc2.12-x86_64/
datadir=/my_mysql/3306/data
log-bin=/my_mysql/3306/mysql-bin
server-id=1
[mysqld_safe]
log-error=/my_mysql/3306/mysql_3306_error.log
pid-file=/my_mysql/3306/mysqld_3306.pid
[client]
3307
[mysqld]
port=3307
socket=/my_mysql/3307/mysql.sock
basedir=/application/mysql-5.6.43-linux-glibc2.12-x86_64/
datadir=/my_mysql/3307、data
log-bin=/my_mysql/3307/mysql-bin
server-id=2
[mysqld_safe]
log-error=/my_mysql/3307/mysql_3307_error.log
pid-file=/my_mysql/3307/mysqld_3307.pid
mysql启停脚本
3306
root@160-116-208-21 3306]# vim mysql_3306
port=3307
mysql_user="mysql"
Cmdpath="/application/mysql-5.6.43-linux-glibc2.12-x86_64/bin/"
mysql_sock="/my_mysql/${port}/mysql.sock"
mysqld_pid_file_path=/my_mysql/${port}/mysqld_${port}.pid
start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${Cmdpath}/mysqld_safe --defaults-file=/my_mysql/${port}/my.cnf --pid-file=$mysqld_pid_file_path
2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}
restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac
赋予可执行权限
chmod +x mysql_3306
3307同理 改端口就行
用户、组授权
降低权限,全部赋予给mysql
[root@160-116-208-21 3307]# chown -R mysql.mysql /my_mysql/
PATH配置
[root@160-116-208-21 ~]# tail -1 /etc/profile
export PATH=/application/mysql-5.6.43-linux-glibc2.12-x86_64/bin/:$PATH
[root@160-116-208-21 ~]# echo $PATH
/application/mysql-5.6.43-linux-glibc2.12-x86_64/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@160-116-208-21 ~]# which mysql
/application/mysql-5.6.43-linux-glibc2.12-x86_64/bin/mysql
创建多个实例对应的数据目录
分别创建3306 3307 2个实例的数据目录
[root@160-116-208-21 ~]# mkdir -p /my_mysql/3306/data
[root@160-116-208-21 ~]# mkdir -p /my_mysql/3307/data
[root@160-116-208-21 ~]# tree /my_mysql/
/my_mysql/
├── 3306
│ ├── data
│ ├── my.cnf
│ └── mysql_3306
└── 3307
├── data
├── my.cnf
└── mysql_3307
mysql多实例初始化
3306
/application/mysql-5.6.43-linux-glibc2.12-x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3306/my.cnf --basedir=/application/mysql-5.6.43-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3306/data/ --user=mysql
3307
/application/mysql-5.6.43-linux-glibc2.12-x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3307/my.cnf --basedir=/application/mysql-5.6.43-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3307/data/ --user=mysql
出现2个ok,表示正常
此时会正确生成mysql的初始数据
[root@160-116-208-21 data]# ls -l /my_mysql/3307/data/
总用量 110600
-rw-rw---- 1 mysql mysql 12582912 8月 18 15:06 ibdata1
-rw-rw---- 1 mysql mysql 50331648 8月 18 15:06 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 8月 18 15:06 ib_logfile1
drwx------ 2 mysql mysql 4096 8月 18 15:06 mysql
drwx------ 2 mysql mysql 4096 8月 18 15:06 performance_schema
drwx------ 2 mysql mysql 6 8月 18 15:06 test
分别启动
[root@160-116-208-21 3306]# /my_mysql/3306/mysql_3306 start
Starting MySQL...
登录数据库
[root@160-116-208-21 3306]# mysql -S /my_mysql/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.