1.1 多实例讲解
1.1.1 创建环境配置文件
3306 3307 3308
多个mysqld进程(mysqld_safe)
多个配置文件(port、server_id datadir log-error log-bin socket)
管理多套数据(多个数据目录,分别初始化数据)
1.1.2 创建目录
创建mkdir -p /data/{3306,3307,3308}
多实例:
3306 3307 3308
多个mysqld 进程(mysqld_safe)
多个配置文件(port server_id datadir log-error log-bin socket)
管理多套数据(多个数据目录,分别初始化数据)
mkdir -p {3306,3307,3308}
1.1.3 创建配置文件
vim /data/3306/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3306/data
server-id=3306
port=3306
log-bin=/data/3306/mysql-bin
socket=/data/3306/mysql.sock
log-error=/data/3306/mysql.log
vim /data/3307/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
server-id=3307
port=3307
log-bin=/data/3307/mysql-bin
socket=/data/3307/mysql.sock
log-error=/data/3307/mysql.log
vim /data/3308/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
server-id=3308
port=3308
log-bin=/data/3308/mysql-bin
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
1.1.4 目录授权
chown -R mysql.mysql /data/
1.1.5 初始化数据
cd /application/mysql/scripts
./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
./mysql_install_db --defaults-file=/data/3308/my.cnf --basedir=/application/mysql --datadir=/data/3308/data --user=mysql
1.1.6 启动服务
/application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf &
/application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf &
/application/mysql/bin/mysqld_safe --defaults-file=/data/3308/my.cnf &
1.1.7 查看是否启动
[root@db02 /data/3307/data]$netstat -lnp|grep 330
tcp 0 0 :::3306 :::* LISTEN 3877/mysqld
tcp 0 0 :::3307 :::* LISTEN 4048/mysqld
tcp 0 0 :::3308 :::* LISTEN 4219/mysqld
unix 2 [ ACC ] STREAM LISTENING 18009 3877/mysqld /data/3306/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 18137 4048/mysqld /data/3307/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 18265 4219/mysqld /data/3308/mysql.sock
1.1.8 登录多实例
说明:登录可以输入下面的命令或者是
[root@db02 ~]$mysql -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.18 sec)
说明:以下命令也可操作:
mysql -uroot -p -P3308
mysql -uroot -p -S /tmp/mysql3307.sock
1.1.9 多实例关闭方法
说明:在多实例关闭的时候指定关闭的端口即可。
[root@db02 /]$mysqladmin shutdown -S /data/3306/mysql.sock
[root@db02 /]$ps -ef|grep 330
root 1330 1 0 08:49 ? 00:00:00 /usr/sbin/acpid
root 2520 2088 0 22:25 pts/1 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf
mysql 2815 2520 0 22:25 pts/1 00:00:12 /application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf --basedir=/application/mysql --datadir=/data/3307/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3307/mysql.log --pid-file=/data/3307/data/db02.pid --socket=/data/3307/mysql.sock --port=3307
root 2879 2088 0 22:25 pts/1 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3308/my.cnf
mysql 3028 2879 0 22:25 pts/1 00:00:10 /application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf --basedir=/application/mysql --datadir=/data/3308/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3308/mysql.log --pid-file=/data/3308/data/db02.pid --socket=/data/3308/mysql.sock --port=3308
root 3288 3146 0 23:14 pts/2 00:00:00 grep 330
备注:以下命令
关闭MySQL数据库
# mysqld_multi stop 3306
# mysqld_multi stop 3307,3308,3309
# ps -ef | grep mysqld
root 30822 12043 0 23:04 pts/0 00:00:00 grep mysqld