MySQL多实例
- 一、多实例概述
- 二、配置多实例
- 2.1、软件环境
- 2.2、配置文件
- 2.3、管理多实例
- 三、客户端访问
一、多实例概述
1.1、什么是多实例
1.2、多实例优点
二、配置多实例
2.1、软件环境
- 解压软件
[root@host58 ~]# rpm -q libaio
libaio-0.3.109-13.el7.x86_64
[root@host58 ~]# ls
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@host58 ~]# tar -zxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@host58 ~]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
[root@host58 ~]# ls /usr/local/mysql
- 修改PATH路径
[root@host58 ~]# PATH=/usr/local/mysql/bin:$PATH
[root@host58 ~]# vim /etc/bashrc
export PATH=/usr/local/mysql/bin:$PATH
[root@host58 ~]# source /etc/bashrc
2.2、配置文件
- /etc/my.cnf
- 每个实例要有独立的数据库目录、端口
- socket文件、pid文件、错误日志文件
[root@host58 ~]#vim /etc/my.cnf
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
user=root
[mysqld1]
datadir=/dir1
port=3307
log-error=/dir1/mysqld1.error
pid-file=/dir1/mysqld1.pid
socket=/dir1/mysqld1.sock
[mysqld2]
datadir=/dir2
port=3308
log-error=/dir2/mysqld2.error
pid-file=/dir2/mysqld2.pid
socket=/dir2/mysqld2.sock
2.3、管理多实例
- 先创建一个mysql用户
[root@host58 ~]# useradd mysql
- 启动MySQL数据库1、2
可以获取到数据库初始密码
[root@host58 ~]# mysqld_multi start 1
root@localhost: pXPUu9ufMx,f
[root@host58 ~]# mysqld_multi start 2
root@localhost:K)UDhR8fqtwt
- 查看端口
[root@host58 ~]# netstat -utnlp | grep mysql
tcp6 0 0 :::3307 :::* LISTEN 6576/mysqld
tcp6 0 0 :::3308 :::* LISTEN 6762/mysqld
- 登录数据库1
[root@host58 ~]
mysql> alter user root@"localhost" identified by "123456";
Query OK, 0 rows affected (0.04 sec)
mysql> show databases;
mysql> create database bbsdb;
- 登录数据库2
[root@host58 ~]
mysql> alter user root@"localhost" identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> create database gamedb;
- 停止数据库2
[root@host58 ~]# mysqld_multi --user=root --password=123456 stop 2
[root@host58 ~]# netstat -utnlp | grep mysqld
tcp6 0 0 :::3307 :::* LISTEN 6576/mysqld
三、客户端访问
- 分别再实例1和实例2上做用户授权
实例1
mysql> grant all on bbsdb.* to sql1@"%" identified by "123456";
实例2
mysql> grant all on gamedb.* to sql2@"%" identified by "123456";
- 客户端登录实例2
[root@client ~]# mysql -h192.168.4.58 -P3308 -usql2 -p123456
mysql> show databases;