一台mysql服务器启动多个端口

一、首先要先把my.cnf配置文件复制一份,开几个端口要复制几份,并且要重新命名。

cp /etc/my.cnf /etc/my3307.cnf

二、 修改/etc/my3307.cnf 配置文件

1
2
3
4
5
6
7
8
9
10
11
[client]
port = 3307
socket = /tmp/mysql3307.sock
default-character-set=UTF
[mysqld]
user = root
port = 3307
basedir = /alidata/server/mysql
datadir = /alidata/server/mysql3307/data
socket = /tmp/mysql3307.sock
log-error=/alidata/log/mysql3307/error.log

三、创建数据库指定存放数据的目录

1
mkdir /alidata/server/mysql3307/data

四、创建log日志存放目录

1
mkdir /alidata/log/mysql3307/

五、初始化数据库
进入到/alidata/server/mysql目录下,执行以下命令:

1
/alidata/server/mysql-5.6.21/scripts/mysql_install_db --defaults-extra-file=/etc/my3307.cnf

执行这条命令后如果提示个别文件在/根目录下找不到的情况,例如:
fill_help_tables.sql,mysql_security_commands.sql,mysql_system_tables_data.sql,mysql_system_tables.sql
就把这些文件拷贝到根目录下

六、启动mysql,要指定.cnf文件

1
/alidata/server/mysql-5.6.21/bin/mysqld_safe --defaults-extra-file=/etc/my3307.cnf  &

七、连接数据库

1
mysql -S /tmp/mysql3307.sock -P 3307 使用这句命令进行连接

但是可能会出现连接不上的情况
error: ‘Access denied for user ‘root‘@’localhost’ (using password: YES)’

解决方案:

1、停止数据库,杀掉mysql端口为3307的进程

1
2
ps -ax | grep mysql
kill 22139 #找到相应的进程号

2、启动时跳过授权

1
/alidata/server/mysql-5.6.21/bin/mysqld_safe --defaults-extra-file=/etc/my3307.cnf --skip-grant-tables &

3、连接数据库

1
mysql -S /tmp/mysql3307.sock -P 3307

4、连接mysql后,对用户进行授权

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root#mysql' WITH GRANT OPTION;

但是在授权时,可能会提示错误
ERROR 1290 (HY000): The MySQL server is running with the –skip-grant-tables option so it cannot execute this statement

解决方法如下:

(1)执行:

1
flush privileges;

(2)再执行:

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root#mysql' WITH GRANT OPTION;

就可以授权成功了

5、退出mysql后,在停掉mysql端口3307的数据库

6、重新执行启动命令

1
/alidata/server/mysql-5.6.21/bin/mysqld_safe --defaults-extra-file=/etc/my3307.cnf  &

7、mysql 端口3307 数据库创建成功