多实例的好处
数据库实例1,目录是
/my_mysql/3306/
/my_mysql/3306/data
/my_mysql/3306/my.cnf
/my_mysql/3306/mysqld
数据库实例3,目录是
/my_mysql/3307/
/my_mysql/3307/data
/my_mysql/3307/my.cnf
/my_mysql/3307/mysqld
不同的应用程序,读取不同的mysql实例
部署MySQL多实例
基于一个MySQL应用
初始化3次,生成3个独立的MySQL数据目录,即为三个MySQL独立的实力
如何准备MySQL应用
二进制方式安装MySQL
一、二进制方式安装MySQL
1. 获取二进制代码包
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
2.安装MySQL运行所需的基础依赖
yum install ncurses-devel libaio-devel gcc make cmake -y
3.清空之前编译安装MySQL,配置的环境的清理
清空PATH有关的MySQL
[root@centOS78 ~]
/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
4.停止当前Linux的MySQL(若是存在MySQL端口的话)
[root@centOS78 ~]
Shutting down MySQL.. SUCCESS!
5.创建MySQL用户
[root@centOS78 ~]
uid=27(mysql) gid=27(mysql) groups=27(mysql)
6.准备好MySQL多实例的数据目录
[root@centOS78 ~]
[root@centOS78 ~]
/my_mysql/
├── 3306
└── 3307
2 directories, 0 files
7.解压缩进二进制的MySQL软件包
注意:-C 指定目录压缩
[root@centOS78 ~]
8.解压缩最后一步,查看MySQL的安装包信息
[root@centOS78 ~]
[root@centOS78 ~]
total 0
9.刚才解压缩的二进制MySQL暗转目录
drwxr-xr-x 13 root root 191 May 12 14:59 mysql-5.6.40-linuxglibc2.12-x86_64
一个简单的软连接
lrwxrwxrwx 1 root root 26 May 9 15:49 mysql ->/application/mysql-5.6.40/
二、准备二进制MySQL运行所需的环境
1.准备MySQL多实例的,各个配置文件
3306
3307
2.准备各个启停管理脚本
3.数据初始化,生成MySQL的初始化data数据
三、准备多个实例的配置文件
1.实例1:3306的配置文件
[root@centOS78 ~]
[client]
[mysqld]
port=3306
socket=/my_mysql/3306/mysql.sock
basedir=/application/mysql-5.6.40-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
1.实例1:3306的配置文件
[root@centOS78 ~]
[client]
[mysqld]
port=3307
socket=/my_mysql/3307/mysql.sock
basedir=/application/mysql-5.6.40-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
四、3306 3307的启停脚本
注意:3306和3307这2个实例,配置文件也得区别开
1.在3306下配置mysql_3306
[root@centOS78 3306]
port=3306
mysql_user="mysql"
Cmdpath="/application/mysql-5.6.40-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
注意:
赋予可执行权限
[root@centOS78 3306]
2.在3307下配置mysql_3307
[root@centOS78 3307]
port=3307
mysql_user="mysql"
Cmdpath="/application/mysql-5.6.40-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
注意:
赋予可执行权限
[root@centOS78 3307]
五、降低权限,全部赋予给MySQL
[root@centOS78 3307]
六、PATH配置
root@centOS78 ~]
export PATH=/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin/:$PATH
root@centOS78 ~]
/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
root@centOS78 ~]
/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin/mysql
七、创建多个实例对应的数据目录
分别创建 3306 3307
2个目录的数据目录
root@centOS78 ~]
root@centOS78 ~]
root@centOS78 ~]
/my_mysql/
├── 3306
│ ├── data
│ ├── my.cnf
│ └── mysql_3306
└── 3307
├── data
├── my.cnf
八、见证MySQL的多实例初始化
1.初始化3306的数据
此时3306的data文件夹是空的,没有数据
root@centOS78 ~]
执行初始化,生成MySQL运行所需的初始化数据
/application/mysql-5.6.40-linux-glibc2.12-x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3306/my.cnf --basedir=/application/mysql-5.6.40-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3306/data/ --user=mysql
注意:
出现两个OK后表示正常
此时会正确生成MySQL的初始数据
root@centOS78 ~]
total 110604
-rw-rw---- 1 mysql mysql 56 Aug 27 20:47 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 Aug 27 21:44 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Aug 27 21:44 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Aug 27 19:50 ib_logfile1
drwx------ 2 mysql mysql 20 Aug 27 21:15 lin1
drwx------ 2 mysql mysql 4096 Aug 27 19:50 mysql
drwx------ 2 mysql mysql 4096 Aug 27 20:29 performance_schema
drwx------ 2 mysql mysql 6 Aug 27 19:50 test
2.初始化3307的数据
此时3306的data文件夹是空的,没有数据
root@centOS78 ~]
执行初始化,生成MySQL运行所需的初始化数据
/application/mysql-5.6.40-linux-glibc2.12-x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3307/my.cnf --basedir=/application/mysql-5.6.40-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3307/data/ --user=mysql
注意:
出现两个OK后表示正常
此时会正确生成MySQL的初始数据
root@centOS78 ~]
total 110604
-rw-rw---- 1 mysql mysql 56 Aug 27 20:48 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 Aug 27 21:47 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Aug 27 21:47 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Aug 27 19:50 ib_logfile1
drwx------ 2 mysql mysql 4096 Aug 27 19:50 mysql
drwx------ 2 mysql mysql 4096 Aug 27 20:30 performance_schema
drwx------ 2 mysql mysql 6 Aug 27 19:50 test
3.创建MySQL的错误日志文件
root@centOS78 ~]
root@centOS78 ~]
注意:
再次对整个目录授权
root@centOS78 ~]
九、分别启动MySQL多实例
1.启动3306数据库
确保当前没有其他MySQL
root@centOS78 ~]
root@centOS78 ~]
Usage: /data/3306/mysql{start|stop|restart}
root@centOS78 ~]
MySQL is running...
用如下命令登录,使用sock套接字文件登录
root@centOS78 ~]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
mysql>
mysql>
mysql>
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> show databases;
2.启动3307数据库
确保当前没有其他MySQL
root@centOS78 ~]
root@centOS78 ~]
Usage: /data/3307/mysql{start|stop|restart}
root@centOS78 ~]
MySQL is running...
用如下命令登录,使用sock套接字文件登录
root@centOS78 ~]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
mysql>
mysql>
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> show databases;