在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 mysqld_multi
1.修改my.cnf
如一个定义两个实例的参考配置:
- [mysqld_multi]
- mysqld = /usr/local/mysql/bin/mysqld_safe
- mysqladmin = /usr/local/mysql/bin/mysqladmin
- user = your_user
- password = your_password
- [mysqld1]
- datadir = /data/db/my1
- #连接
- port = 3306
- socket = /tmp/mysql3306.sock
- #binlog
- log-bin=/data/db/mylog1/mysql-bin
- binlog_format=mixed
- binlog_cache_size = 32M
- expire_logs_days = 30
- [mysqld2]
- datadir = /data/db/my2
- #连接
- port = 3307
- socket = /tmp/mysql3307.sock
- #binlog
- log-bin=/data/db/mylog2/mysql-bin
- binlog_format=mixed
- binlog_cache_size = 32M
- expire_logs_days = 3
2.创建数据目录
- mkdir -p /data/db/my21
- mkdir -p /data/db/my2
- chown mysql.mysql /data/db/my1 -R
- chown mysql.mysql /data/db/my2 -R
3.初始化DB
- /usr/local/mysql/scripts/mysql_install_db --datadir=/data/db/my1/ -uroot (mysql_install_db也是MYSQL官方自带工具)
- /usr/local/mysql/scripts/mysql_install_db --datadir=/data/db/my2/ -uroot
- chown mysql.mysql /data/db/my1/ -R
- chown mysql.mysql /data/db/my2/ -R
4. 安装工具
- cp /usr/local/mysql/bin/my_print_defaults /usr/bin/
- cp /usr/local/mysql/bin/mysqld_multi /usr/bin/
5.创建、授权用户
- CREATE USER "your_user"@"192.168.1.%" IDENTIFIED BY 'your_password';
- GRANT ALL PRIVILEGES ON *.* TO "your_user"@"192.168.1.%";
- flush privileges;
至此,mysql多实例配置已经完毕。我们看到多个不同的MYSQL实例是共用my.cnf的。多实例命令行管理:
1.mysql启动
- mysqld_multi start 1 启动实例1
- mysqld_multi start 1-2 启动实例1,2
2.mysql重启
- mysqld_multi restart 1 重启实例1
- mysqld_multi restart 1-2 重启实例1,2
3.mysql关闭
- mysqld_multi stop 1 关闭实例1
- mysqld_multi stop 1-2 关闭实例1,2
4.命令行登陆实例2
- mysql -u your_user -p your_password -P3307 -S /tmp/mysql3307.sock