MySQL多实例

1.实例

1.单实例:
    1个进程 + 多个线程 + 单个预分配的内存空间

2.多实例:
    多个进程 + 多个线程 + 多个预分配的内存空间

3.MySQL多实例:
    1)多个端口
    2)多个socket文件
    3)多个server_id
    4)多个日志文件
    5)多个配置文件
    6)多个数据目录

2.创建多实例存放目录

[root@db02 /service]# mkdir /service/{3307,3308,3309}

3.配置多个配置文件

[root@db02 /service]# vim /service/3307/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3307/data
port=3307
socket=/service/3307/data/mysql.sock
server_id=7
log-error=/service/3307/data/mysql.err

[root@db02 /service]# vim /service/3308/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3308/data
port=3308
socket=/service/3308/data/mysql.sock
server_id=8
log-error=/service/3308/data/mysql.err

[root@db02 /service]# vim /service/3309/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3309/data
port=3309
socket=/service/3309/data/mysql.sock
server_id=9
log-error=/service/3309/data/mysql.err

4.初始化多实例数据目录

[root@db02 /service]# cd mysql/scripts/
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3307/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3307/data
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3308/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3308/data
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data

[root@db02 /service]# tree -L 3 ./
./
├── 3307
│   ├── data
│   │   ├── ibdata1
│   │   ├── ib_logfile0
│   │   ├── ib_logfile1
│   │   ├── mysql
│   │   ├── mysql.err
│   │   ├── performance_schema
│   │   └── test
│   └── my.cnf
├── 3308
│   ├── data
│   │   ├── ibdata1
│   │   ├── ib_logfile0
│   │   ├── ib_logfile1
│   │   ├── mysql
│   │   ├── mysql.err
│   │   ├── performance_schema
│   │   └── test
│   └── my.cnf
├── 3309
│   ├── data
│   │   ├── ibdata1
│   │   ├── ib_logfile0
│   │   ├── ib_logfile1
│   │   ├── mysql
│   │   ├── mysql.err
│   │   ├── performance_schema
│   │   └── test
│   └── my.cnf
├── mysql -> /service/mysql-5.6.42
└── mysql-5.6.42

5.授权

[root@db02 /service]# chown -R mysql.mysql /service/

6.启动多实例

[root@db02 /service]# mysqld_safe --defaults-file=/service/3307/my.cnf &
[root@db02 /service]# mysqld_safe --defaults-file=/service/3308/my.cnf &
[root@db02 /service]# mysqld_safe --defaults-file=/service/3309/my.cnf &

7.验证启动

[root@db02 /service]# netstat -lntp | grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      24992/mysqld
tcp6       0      0 :::3307                 :::*                    LISTEN      32069/mysqld 
tcp6       0      0 :::3308                 :::*                    LISTEN      32234/mysqld
tcp6       0      0 :::3309                 :::*                    LISTEN      32394/mysqld

8.登录验证多实例

[root@db02 /service]# mysql -uroot -S /service/3307/data/mysql.sock -e show variables like "server_id"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 7     |
+---------------+-------+

[root@db02 /service]# mysql -uroot -S /service/3308/data/mysql.sock -e show variables like "server_id"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 8     |
+---------------+-------+

[root@db02 /service]# mysql -uroot -S /service/3309/data/mysql.sock -e show variables like "server_id"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 9     |
+---------------+-------+

9.多实例设置密码

[root@db02 /service]# mysqladmin -uroot password -S /service/3307/data/mysql.sock
New password: 123
Confirm new password: 123
[root@db02 /service]# mysqladmin -uroot password -S /service/3308/data/mysql.sock
New password: 123
Confirm new password: 123
[root@db02 /service]# mysqladmin -uroot password -S /service/3309/data/mysql.sock
New password: 123
Confirm new password: 123

10.配置简单连接数据库多实例

#1.编写连接数据库文件
[root@db02 /service]# cat /usr/bin/mysql3307
mysql -uroot -p123 -S /service/3307/data/mysql.sock
[root@db02 /service]# cat /usr/bin/mysql3308
mysql -uroot -p123 -S /service/3308/data/mysql.sock
[root@db02 /service]# cat /usr/bin/mysql3309
mysql -uroot -p123 -S /service/3309/data/mysql.sock

#2.授权执行权限
[root@db02 /service]# chmod +x /usr/bin/mysql*

#3.直接使用命令连接
[root@db02 /service]# mysql3307
[root@db02 /service]# mysql3308
[root@db02 /service]# mysql3309

 

MySQL多实例

上一篇:MySQL_密码重置


下一篇:锋利的jQuery学习第二章