在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 mysqld_multi
1.修改my.cnf
如一个定义两个实例的参考配置

  1. [mysqld_multi]

  2. mysqld = /usr/local/mysql/bin/mysqld_safe

  3. mysqladmin = /usr/local/mysql/bin/mysqladmin

  4. user = your_user

  5. password = your_password

  6. [mysqld1]

  7. datadir = /data/db/my1

  8. #连接

  9. port = 3306

  10. socket = /tmp/mysql3306.sock

  11. #binlog

  12. log-bin=/data/db/mylog1/mysql-bin

  13. binlog_format=mixed

  14. binlog_cache_size = 32M

  15. expire_logs_days = 30

  16. [mysqld2]

  17. datadir = /data/db/my2

  18. #连接

  19. port = 3307

  20. socket = /tmp/mysql3307.sock

  21. #binlog

  22. log-bin=/data/db/mylog2/mysql-bin

  23. binlog_format=mixed

  24. binlog_cache_size = 32M

  25. expire_logs_days = 3

复制代码



2.创建数据目录

  1. mkdir -p /data/db/my21

  2. mkdir -p /data/db/my2

  3. chown mysql.mysql /data/db/my1 -R

  4. chown mysql.mysql /data/db/my2 -R

复制代码



3.初始化DB

  1. /usr/local/mysql/scripts/mysql_install_db --datadir=/data/db/my1/ -uroot (mysql_install_db也是MYSQL官方自带工具)

  2. /usr/local/mysql/scripts/mysql_install_db --datadir=/data/db/my2/ -uroot

  3. chown mysql.mysql /data/db/my1/ -R

  4. chown mysql.mysql /data/db/my2/ -R

复制代码



4. 安装工具

  1. cp /usr/local/mysql/bin/my_print_defaults /usr/bin/

  2. cp /usr/local/mysql/bin/mysqld_multi /usr/bin/

复制代码



5.创建、授权用户

  1. CREATE USER "your_user"@"192.168.1.%" IDENTIFIED BY 'your_password';

  2. GRANT ALL PRIVILEGES ON *.* TO "your_user"@"192.168.1.%";

  3. flush privileges;

复制代码




至此,mysql多实例配置已经完毕。我们看到多个不同的MYSQL实例是共用my.cnf的。多实例命令管理
1.mysql启动

  1. mysqld_multi start 1 启动实例1

  2. mysqld_multi start 1-2 启动实例1,2

复制代码


2.mysql重启
  1. mysqld_multi restart 1 重启实例1

  2. mysqld_multi restart 1-2 重启实例1,2

复制代码



3.mysql关闭

  1. mysqld_multi stop 1 关闭实例1

  2. mysqld_multi stop 1-2 关闭实例1,2

复制代码



4.命令行登陆实例2

  1. mysql -u your_user -p your_password -P3307 -S /tmp/mysql3307.sock

复制代码