在测试mysql主从服务器的时候,在一个虚拟机开启多端口同步不同主库。

详细步骤:

  1.首先下载个mysql5.6的64位二进制源码包:http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz

  2.tar zxvf mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz解压

  3.mv mysql-5.6.27 /usr/local/mysql

  4.cp /etc/my.cnf /etc/my3301.cnf

   cp /etc/my.cnf /etc/my3302.cnf复制my.cnf的配置文件,开几个端口就复制几份,这里开启3301和3302端口。

  5.修改/etc/my3301.cnf和my3302.cnf文件

  如:

[client]
port = 3301
socket = /tmp/mysql3301.sock
default-character-set=UTF
[mysqld]
port = 3301
socket = /tmp/mysql3301.sock
basedir=/usr/local/mysql
datadir=/data/mysql1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

需要修改datadir,port和socket

  6.创建数据库指定存放数据的目录

mkdir /data/mysql1
mkdir /data/mysql2

  7.初始化数据库

/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql1 --user=mysql --basedir=/usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql2 --user=mysql --basedir=/usr/local/mysql

  8.启动mysql

 /usr/local/mysql/bin/mysqld_safe --defaults-extra-file=/etc/my3301.cnf --datadir=/data/mysql1 --user=mysql &
 /usr/local/mysql/bin/mysqld_safe --defaults-extra-file=/etc/my3302.cnf --datadir=/data/mysql2 --user=mysql &

  9.netstat -lnp 查看mysql是否启动

 10.停止mysql,对应sock文件

/usr/local/mysql/bin/mysqladmin -uroot -S /tmp/mysql3301.sock shutdown

 11.使用mysql

 mysql -S /tmp/mysql3302.sock -P 3302

另外可以用mysqld_multi管理

vim /etc/my.cnf

[mysqld1]

basedir = /usr/local/mysql

user = mysql

datadir = /data/mysql1

port = 3301

# server_id = .....

socket = /tmp/mysql1.sock

pid-file = /data/mysql1/mysql1.pid

[mysqld2]

user = mysql

datadir = /data/mysql2

basedir = /usr/local/mysql

port = 3302

# server_id = .....

socket = /tmp/mysql2.sock

pid-file = /data/mysql2/mysql2.pid

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

就可以用/usr/local/mysql/bin/mysqld_multi start/stop/reload/report 0或1

需要注意的是要将/usr/local/mysql/bin加入到PATH里