1. 程式人生 > >MySQL多實例

MySQL多實例

right ffi tom netstat ron list span ica mman

1.1 多實例講解

1.1.1 創建環境配置文件

3306 3307 3308

多個mysqld進程(mysqld_safe

多個配置文件(portserver_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


MySQL多實例