前提准备

做测试还是采用二进制方式安装,简单方便快速,在原环境安装好的基础上。    
MySQL-5.6.x二进制版本安装记录 http://koumm.blog.51cto.com/703525/1762167    
个人不喜欢采用mysqlmulti方式配置多实例,还是采用多实例多进程方式配置。

1, 准备数据库实例datadir目录

# mkdir -p /usr/local/mysql/mysql3307    
# chown mysql:mysql /usr/local/mysql/mysql3307

初始化实例目录    
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql3307

2. 准备配置文件

(1) 修改现有配置文件名my.cnf修为3306.cnf,并复制一个配置文件名为3307.cnf    
(2) 3306.cnf配置文件修改,端口,sock位置,pid位置,datadir位置等。

[client]    
port        = 3306    
socket      = /tmp/mysql3306.sock

[mysqld]    
port        = 3306    
socket      = /tmp/mysql3306.sock    
datadir     = /usr/local/mysql/data


(3) 3307.cnf配置文件修改

[client]    
port        = 3307    
socket      = /tmp/mysql3307.sock

[mysqld]    
port        = 3307    
socket      = /tmp/mysql3307.sock    
datadir     = /usr/local/mysql/mysql3307

 

3. 启动多实例,启动脚本可以加入到开机自启动文件中。

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/3306.cnf 2>&1 >/dev/null &    
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/3307.cnf 2>&1 >/dev/null &

 

4. 数据库登录

# /usr/local/mysql/bin/mysql -u root -p -P 3306 -S /tmp/mysql3306.sock    
# /usr/local/mysql/bin/mysql -u root -p -P 3307 -S /tmp/mysql3307.sock

修改3307实例密码:    
# /usr/local/mysql/bin/mysqladmin -u root password 'admin' -S /tmp/mysql3307.sock  #设置管理员密码

 

3306登录示例1:

[root@test ~]# mysql -uroot -p -S /tmp/mysql3306.sock    
Enter password:    
Welcome to the MySQL monitor.  Commands end with ; or \g.    
Your MySQL connection id is 14    
Server version: 5.6.24-log Source distribution

Copyright (c) 2000, 2015, 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>    
mysql> \q    
Bye

3307登录示例:

[root@test ~]# mysql -uroot -p  -S /tmp/mysql3307.sock    
Enter password:    
Welcome to the MySQL monitor.  Commands end with ; or \g.    
Your MySQL connection id is 15    
Server version: 5.6.24-log Source distribution

Copyright (c) 2000, 2015, 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>

指定端口号也是可以访问的:

mysql -uroot -p -h127.0.0.1 -P3307

5. 关闭mysql多实例方式

# /usr/local/mysql/bin/mysqladmin -u root -p -P 3306 -S /tmp/mysql3306.sock shutdown    
# /usr/local/mysql/bin/mysqladmin -u root -p -P 3307 -S /tmp/mysql3307.sock shutdown

或采用kill方式,或编写启动脚本。