Mysql多个端口设置

一个Mysql(5.5版本)设置多个端口运行多个实例。搞了一天,终于弄好,先备忘一下!

一、设置mysqld_multi

复制一份my.cnf,重命名为my_multi.cnf

修改my_multi.cnf,主要配置项目如下

[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user       = root
password   = 123

#默认的3306端口
[mysqld6]
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp

#加一个3308的端口
[mysqld8]
user		= mysql
pid-file	= /var/run/mysqld/mysqld8.pid
socket		= /var/run/mysqld/mysqld8.sock
port		= 3308
basedir		= /usr
datadir		= /var/lib/mysql8
tmpdir		= /tmp


二、创建3308端口的数据存储目录

mkdir /var/lib/mysql8
chown mysql:mysql /var/lib/mysql8

三、修改文件/etc/apparmor.d/usr.sbin.mysqld

        修改完成后重启:/etc/init.d/apparmor restart


/usr/sbin/mysqld {
  #include <abstractions/base>
  #include <abstractions/nameservice>
  #include <abstractions/user-tmp>
  #include <abstractions/mysql>
  #include <abstractions/winbind>

  capability dac_override,
  capability sys_resource,
  capability setgid,
  capability setuid,

  network tcp,

  /etc/hosts.allow r,
  /etc/hosts.deny r,

  /etc/mysql/*.pem r,
  /etc/mysql/conf.d/ r,
  /etc/mysql/conf.d/* r,
  /etc/mysql/*.cnf r,
  /usr/lib/mysql/plugin/ r,
  /usr/lib/mysql/plugin/*.so* mr,
  /usr/sbin/mysqld mr,
  /usr/share/mysql/** r,
  /var/log/mysql.log rw,
  /var/log/mysql.err rw,
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,
  /var/log/mysql/ r,
  /var/log/mysql/* rw,
  /var/run/mysqld/mysqld.pid rw,
  /var/run/mysqld/mysqld.sock w,
  /run/mysqld/mysqld.pid rw,
  /run/mysqld/mysqld.sock w,
  

  #####主要是添加这些读写权限信息,不然下一步骤初始化数据库时会报无权限错误  
  /var/lib/mysql8/ r,
  /var/lib/mysql8/** rwk,
  /var/run/mysqld/mysqld8.pid rw,
  /var/run/mysqld/mysqld8.sock w,
  /run/mysqld/mysqld8.pid rw,
  /run/mysqld/mysqld8.sock w,
  
  

  /sys/devices/system/cpu/ r,

  # Site-specific additions and overrides. See local/README for details.
  #include <local/usr.sbin.mysqld>
}


四、初始化数据库

mysql_install_db --user=mysql --datadir=/var/lib/mysql8 &


五、重启数据库(默认打开3306端口的数据库)

service mysql restart

六、打开3308端口的数据库

 mysqld_multi --defaults-file=/etc/mysql/my_multi.cnf start 8

七、完事,可以查看下了

netstat -ntlp

  终于看到可喜的结果如下!

tcp    0      0 127.0.0.1:3306     0.0.0.0:*     LISTEN      1402/mysqld
tcp    0      0 127.0.0.1:3308     0.0.0.0:*     LISTEN      2202/mysqld


  1. 启动:mysqld_multi --defaults-file=/etc/mysql/my_multi.cnf start 8

  2. 登陆:mysql -S /var/run/mysqld/mysq8.sock -P 3308

  3. 关闭:mysqladmin -uroot -S /var/run/mysqld/mysqld8.sock shutdown -p


你可能感兴趣的:(Mysql多个端口设置)