如何在具有不同端口的同一台机器上配置 MySQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19095580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure MySQL on same machine with different port?
提问by NullPointer
How to configure two different port for MySQL on same machine? I know that default port is 3306. But I also want to configure 3307 and 3308. Is it possible?
如何在同一台机器上为 MySQL 配置两个不同的端口?我知道默认端口是3306,但是我也想配置3307和3308,可以吗?
One bonus question with current one ;)
一个奖金问题与当前的问题 ;)
Can we assign Different Ports to Different Databases and also can assign/create Credentials respectively?
我们可以为不同的数据库分配不同的端口,也可以分别分配/创建凭证吗?
回答by Ivaylo Strandjev
回答by fjammes
You can launch several instance of mysql :
您可以启动多个 mysql 实例:
mysqld_safe --defaults-file=/path/to/my.cnf1
mysqld_safe --defaults-file=/path/to/my.cnf2
mysqld_safe --defaults-file=/path/to/my.cnf3
and change the portparameter in the [mysqld] section of each my.cnf.
并更改每个 my.cnf 的 [mysqld] 部分中的端口参数。
If you want to have only one mysqld process/database you can use port redirection (with linux) :
如果您只想拥有一个 mysqld 进程/数据库,您可以使用端口重定向(使用 linux):
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3307 -j REDIRECT --to-port 3306
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3308 -j REDIRECT --to-port 3306
回答by OneOfOne
You can copy /etc/mysql/my.cnf
, change the port in it and change the pathes to data dirs as well, because i'm pretty sure You can't have more than 1 instance of mysql serving from the same directories.
您可以复制/etc/mysql/my.cnf
,更改其中的端口并更改数据目录的路径,因为我很确定您不能从同一目录提供超过 1 个 mysql 实例。
Check http://dev.mysql.com/doc/refman/5.1/en/multiple-servers.html.
检查http://dev.mysql.com/doc/refman/5.1/en/multiple-servers.html。
ex :
前任 :
cp /etc/mysql/my.cnf /etc/mysql/my-3307.cnf
//edit my-3307.cnf, for example
port = 3307
basedir = /var/lib/mysql-3307
datadir = /var/lib/mysql-3307
//end-edit
mysql_upgrade --defaults-file=/etc/mysql/my-3307.cnf #checks the syntax and creates the dirs you need.
#start mysqld
mysqld --defaults-file=/etc/mysql/my-3307.cnf
回答by kqw
mysqld_multiis by far the best way to handle different instances of mysql.
mysqld_multi是迄今为止处理不同 mysql 实例的最佳方式。
Some more useful tips:
一些更有用的提示:
mysqld_multi --example
- check if apparmor isn't keeping mysql from accessing
/var/lib/mysql2
if you get weird permission errors.
mysqld_multi --example
/var/lib/mysql2
如果您遇到奇怪的权限错误,请检查 apparmor 是否不会阻止 mysql 访问。
回答by Rodrigo Prazim
I'm using CentOS. Not is too simple,
我正在使用 CentOS。不是太简单,
- Edit file /etc/my.cnf, Search and change or add line: port=port_number.
- semanage port -a -t mysqld_port_t -p tcp port_number
- Restart MySQL Server. service mysqld restart
- 编辑文件/etc/my.cnf,搜索并更改或添加行: port= port_number。
- semanage port -a -t mysqld_port_t -p tcp port_number
- 重新启动 MySQL 服务器。服务 mysqld 重启