多实例介绍和方案
多实例介绍
- 什么是数据库多实例
- MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307等),同时运 行多个MySQL服务进程,这些服务进程通过不同的Socket监听不同的服务端口来提供服务。
- 多实例可能是MySQL的不同版本,也可能是MySQL的同一版本实现
- 多实例的好处
- 可有效利用服务器资源。当单个服务器资源有剩余时,可以充分利用剩余资源提供更多的服务,且 可以实现资源的逻辑隔离节约服务器资源。例如公司服务器资源紧张,但是数据库又需要各自尽量 独立的提供服务,并且还需要到主从复制等技术,多实例就是最佳选择
- 多实例弊端
- 存在资源互相抢占的问题。比如:当某个数据库实例并发很高或者SQL查询慢时,整个实例会消耗 大量的CPU、磁盘I/O等资源,导致服务器上面其他的数据库实例在提供服务的质量也会下降,所以 具体的需求要根据自己的实际情况而定。
MySQL多实例常见的配置方案
- 单一的配置文件、单一启动程序多实例部署方式
- MySQL官方文档提到的单一配置文件、单一启动程序多实例部署方式
- 耦合度太高,一个配置文件不好管理。不是很推荐。
- 多配置文件、多启动程序部署方式
- 多配置文件、多启动程序部署方式是针对每个实例都有独立的配置文件和目录,管理灵活,此方案耦合度较低
- 工作开发和运维的统一原则:降低耦合度。所以建议的此方式。
实战案例 :
CentOS 8 实现mariadb的yum安装的多实例
实战目的
CentOS 8 yum安装mariadb-10.3.17并实现三个实例
环境要求
一台系统CentOS 8.X主机
前提准备
关闭SElinux
关闭防火墙
时间同步
实现步骤
安装mariadb
[root@centos8 ~]#yum -y install mariadb-server
准备三个实例的目录
[root@centos8 ~]#mkdir -pv
/mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
[root@centos8 ~]#chown -R mysql.mysql /mysql
[root@centos8 ~]#tree -d /mysql/
/mysql/
├── 3306
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
├── 3307
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
└── 3308
├── bin
├── data
├── etc
├── log
├── pid
└── socket
21 directories
生成数据库文件
[root@centos8 ~]#mysql_install_db --user=mysql --datadir=/mysql/3306/data
[root@centos8 ~]#mysql_install_db --user=mysql --datadir=/mysql/3307/data
[root@centos8 ~]#mysql_install_db --user=mysql --datadir=/mysql/3308/data
准备配置文件
[root@centos8 ~]#vim /mysql/3306/etc/my.cnf
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
log-error=/mysql/3306/log/mysql.log
pid-file=/mysql/3306/pid/mysql.pid
#重复上面步骤设置3307,3308
[root@centos8 ~]#sed 's/3306/3307/' /mysql/3306/etc/my.cnf >
/mysql/3307/etc/my.cnf
[root@centos8 ~]#sed 's/3306/3308/' /mysql/3306/etc/my.cnf >
/mysql/3308/etc/my.cnf
准备启动脚本
[root@centos8 ~]#vim /mysql/3306/bin/mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="123456"
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${cmd_path}/mysqld_safe --defaultsfile=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock}
shutdown
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
[root@centos8 ~]#chmod +x /mysql/3306/bin/mysqld
#重复上述过程,分别建立3307,3308的启动脚本
启动服务
[root@centos8 ~]#/mysql/3306/bin/mysqld start
[root@centos8 ~]#/mysql/3307/bin/mysqld start
[root@centos8 ~]#/mysql/3308/bin/mysqld start
[root@centos8 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer
Address:Port
LISTEN 0 128 0.0.0.0:22
0.0.0.0:*
LISTEN 0 128 [::]:22
[::]:*
LISTEN 0 80 *:3306
*:*
LISTEN 0 80 *:3307
*:*
LISTEN 0 80 *:3308
*:*
登录实例
[root@centos8 ~]#/mysql/3308/bin/mysqld start
#两种连接方法
[root@centos8 ~]#mysql -h127.0.0.1 -P3308
[root@centos8 ~]#mysql -uroot -S /mysqldb/3306/socket/mysql.sock
#确认连接的端口
MariaDB [(none)]> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3308 |
+---------------+-------+
1 row in set (0.001 sec)
MariaDB [(none)]>
#关闭数据库,需要手动输入root的密码
[root@centos8 ~]#/mysql/3308/bin/mysqld stop
Stoping MySQL...
Enter password:
[root@centos8 ~]#/mysql/3308/bin/mysqld start
Starting MySQL...
修改root密码
#加上root的口令
[root@centos8 ~]#mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password
'magedu'
[root@centos8 ~]#mysqladmin -uroot -S /mysql/3307/socket/mysql.sock password
'magedu'
[root@centos8 ~]#mysqladmin -uroot -S /mysql/3308/socket/mysql.sock password
'magedu'
#或者登录mysql,执行下面也可以
Mariadb>update mysql.user set password=password("centos") where user='root';
Mariadb>flush privileges;
#重复步骤,分别修改别外两个实例3307,3308对应root口令
测试连接
[root@centos8 ~]#mysql -uroot -p -S /mysql/3306/socket/mysql.sock #提示输入口令才能登录
开机启动
[root@centos8 ~]#vi /etc/rc.d/rc.local
#在最后一行加下面内容
for i in {3306..3308};do /mysql/$i/bin/mysqld start;done
[root@centos8 ~]#chmod +x /etc/rc.d/rc.local