除了可以使用service mysql start启动mysql,还可以自己填写参数,同样可以启动数据库。
且还可以指定如下的选项:
basedir datadir pid-file socket defaults-file,那么是不是可以复制两份分别运行呢?当然是可以的,亲。
注意:初始的mysql是通过mysql源码安装的,这样所有的东西都在一起好打包精简之类的。
复制代码 代码示例:
cp -R /usr/local/mysql /usr/local/mysql1
cp -R /usr/local/mysql /usr/local/mysql2
cp -R /usr/local/mysql /usr/local/mysql3
cp -R /usr/local/mysql /usr/local/mysql4
chown -R mysql.mysql /usr/local/mysql1
chown -R mysql.mysql /usr/local/mysql2
chown -R mysql.mysql /usr/local/mysql3
chown -R mysql.mysql /usr/local/mysql4
复制了4份mysql。
接下来我们针对每份mysql做相应的设置。
1、清空mysql1、2、3、4下data目录用的默认数据库,重建
复制代码 代码示例:
/usr/local/mysql1/scripts/mysql_install_db -basedir=/usr/local/mysql1 --datadir=/usr/local/mysql1/data --user=mysql
/usr/local/mysql2/scripts/mysql_install_db -basedir=/usr/local/mysql2 --datadir=/usr/local/mysql2/data --user=mysql
/usr/local/mysql3/scripts/mysql_install_db -basedir=/usr/local/mysql3 --datadir=/usr/local/mysql3/data --user=mysql
/usr/local/mysql4/scripts/mysql_install_db -basedir=/usr/local/mysql4 --datadir=/usr/local/mysql4/data --user=mysql
2、分别修改mysql中的配置文件
复制代码 代码示例:
vi /usr/local/mysql1/my.cnf
port = 3307
socket =/tmp/mysql3307.sock
vi /usr/local/mysql1/my.cnf
port = 3308
socket =/tmp/mysql3308.sock
vi /usr/local/mysql1/my.cnf
port = 3309
socket =/tmp/mysql3309.sock
vi /usr/local/mysql1/my.cnf
port = 3310
socket =/tmp/mysql3310.sock
确认以上四个端口没有被使用
3、编写启动脚本
复制代码 代码示例:
/usr/local/mysql1/bin/mysqld --defaults-file=/usr/local/mysql1/my.cnf --basedir=/usr/local/mysql1 --datadir=/usr/local/mysql1/data --user=mysql --pid-file=/usr/local/mysql1/data/mysql.pid &
/usr/local/mysql2/bin/mysqld --defaults-file=/usr/local/mysql2/my.cnf --basedir=/usr/local/mysql2 --datadir=/usr/local/mysql2/data --user=mysql --pid-file=/usr/local/mysql2/data/mysql.pid &
/usr/local/mysql3/bin/mysqld --defaults-file=/usr/local/mysql3/my.cnf --basedir=/usr/local/mysql3 --datadir=/usr/local/mysql3/data --user=mysql --pid-file=/usr/local/mysql3/data/mysql.pid &
/usr/local/mysql4/bin/mysqld --defaults-file=/usr/local/mysql4/my.cnf --basedir=/usr/local/mysql4 --datadir=/usr/local/mysql4/data --user=mysql --pid-file=/usr/local/mysql4/data/mysql.pid &
4、分别登陆不同的mysql
复制代码 代码示例:
mysql -S /tmp/mysql3307.socket
mysql -S /tmp/mysql3308.socket
mysql -S /tmp/mysql3309.socket
mysql -S /tmp/mysql3310.socket