MySQL多开

在服务器中开启mysql服务一般是用 service mysql start, 但在系统里面他都执行了那些命令吗,通过ps -ef|grep mysql 我们可以看到目标的任务有如:

1
2
3
4
5
/usr/local/Cellar/mysql/5.7.11/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.7.11 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.7.11/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/mysql-error.log --pid-file=/usr/local/var/mysql/mysql.pid
或者可能是:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/3306/mysql_run.err --open-files-limit=65536 --pid-file=/data/mysql/3306/pid_mysql.pid --socket=/data/mysql/3306/mysqltmp/mysql.sock --port=3306

如果我们需要多开,只需要按照这种格式允许下不就好了?

这是个经过优化,在后台多开一个mysql实例的demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql_install_db --datadir=/Volumes/Edu/DB/mysql # mysql 5.7 前使用
mysqld --initialize --datadir=/Volumes/Edu/DB/mysql # mysql 5.7后使用这个初始化
mysqld --user=mysql --basedir=/usr/local/Cellar/mysql/5.7.11 --datadir=/Volumes/Edu/DB/mysql --initial --initialize-insecure --defaults-file=/usr/local/etc/my-3307.cnf --socket=/data/mysql/3306/mysqltmp/mysql.sock
--port=3307
--plugin-dir=/usr/local/Cellar/mysql/5.7.11/lib/plugin
--bind-address=127.0.0.1
--log-error=/usr/local/var/mysql/mysql-error-3307.log --pid-file=/usr/local/var/mysql/mysql-3307.pid
#需要先确保 datadir 目标目录下是空的,避免误操作破坏已有数据。
#另外,在初始化时如果加上 –initial-insecure,则会创建空密码的 root@localhost 账号,否则会创建带密码的 root@localhost 账号,密码直接写在 log-error 日志文件中(在5.6版本中是放在 ~/.mysql_secret 文件里,更加隐蔽,不熟悉的话可能会无所适从)
/usr/local/Cellar/mysql/5.7.11/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.7.11 --datadir=/Volumes/Edu/DB/mysql --plugin-dir=/usr/local/Cellar/mysql/5.7.11/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/mysql-error-3307.log --pid-file=/usr/local/var/mysql/mysql.pid --port=3307

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql-3307.plist