10

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?

One bonus question with current one ;)

Can we assign Different Ports to Different Databases and also can assign/create Credentials respectively?

Smile
  • 2,770
  • 4
  • 35
  • 57

5 Answers5

10

You can use the --port=port_num option. Have a look here for more information on how to configure multiple mysql instances.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
7

You can launch several instance of 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 port parameter in the [mysqld] section of each my.cnf.

If you want to have only one mysqld process/database you can use port redirection (with 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
Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39
  • Can I assign two different ports (3307 and 3308) to two different databases (dba and dbb)? – Smile Sep 30 '13 at 13:39
  • In order to do that, you need to run 2 different mysqld instances, create `dba` in the first one and `dbb` in the second one. – Fabrice Jammes Nov 11 '20 at 21:35
5

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.

Check 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
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
1

mysqld_multi is by far the best way to handle different instances of 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.
the
  • 21,007
  • 11
  • 68
  • 101
0

I'm using CentOS. Not is too simple,

  1. Edit file /etc/my.cnf, Search and change or add line: port=port_number.
  2. semanage port -a -t mysqld_port_t -p tcp port_number
  3. Restart MySQL Server. service mysqld restart
Rodrigo Prazim
  • 788
  • 7
  • 14