CentOS6.5下二进制多实例安装mysql-5.6.27

MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307,3308),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的datadir, port, socket, pid都是不同的。
1、将所有的安装文件、配置文件、数据目录全部放存/mydata/data目录中,便于今后实现快速迁移、整体备份和快速复制;
2、在一台服务器上配置2个MySQL实例,分别绑定在3306、3307端口。

3、每一个实例单独一个my.cnf配置文件:

datadir:    /mydata/data/3306
            /mydata/data/3307
my.cnf:    /mydata/data/3306/my.cnf
           /mydata/data/3307/my.cnf
4、安装MySQL(通用二进制方式)

下面介绍开始安装:

1:添加用户和组

[root@CentOS mysql]# groupadd mysql
[root@CentOS mysql]# useradd -r -g mysql mysql

2:安装mysql到/usr/local/mysql下

[root@CentOS ~]# cd /tmp
[root@CentOS tmp]# ll mysql-5.6.27-linux-glibc2.5-i686.tar.gz 
-rw-r--r--. 1 root root 304056226 11月 17 04:12 mysql-5.6.27-linux-glibc2.5-i686.tar.gz
[root@CentOS tmp]# tar zxf mysql-5.6.27-linux-glibc2.5-i686.tar.gz -C /usr/local/
[root@CentOS tmp]# cd /usr/local/
[root@CentOS local]# mv mysql-5.6.27-linux-glibc2.5-i686/ mysql
[root@CentOS local]# chown -R mysql:mysql mysql/

3:创建MySQL多实例的数据目录

[root@CentOS local]# mkdir /mydata/data/{3306,3307}/data -p
[root@CentOS mysql]# chown -R mysql:mysql /mydata
[root@CentOS data]# tree /mydata/data/
/mydata/data/
├── 3306
│  └── data
└── 3307
    └── data

4 directories, 0 files

4:提供配置文件,并编辑

[root@CentOS data]# cp /usr/local/mysql/support-files/my-default.cnf /mydata/data/3306/my.cnf
[root@CentOS data]# cp /usr/local/mysql/support-files/my-default.cnf /mydata/data/3307/my.cnf
[root@CentOS data]# tree /mydata/data/
/mydata/data/
├── 3306
│   ├── data
│   └── my.cnf
└── 3307
    ├── data
    └── my.cnf

4 directories, 2 files/mydata/data/3306/my.cnf
# 这里是实验环境,所以简单配置。请各位看官根据实际需求调整
[root@CentOS 3306]# vi /mydata/data/3306/my.cnf
[client]
password        = 123456
port            = 3306
socket          = /mydata/data/3306/mysql_3306.sock
default-character-set=utf8

[mysqld]
port            = 3306
socket          = /mydata/data/3306/mysql_3306.sock
pid-file        = /mydata/data/3306/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3306
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
lower_case_table_names=1
max_connections=1000

[root@CentOS 3306]# vi /mydata/data/3307/my.cnf
[client]
password        = 123456
port            = 3307
socket          = /mydata/data/3307/mysql_3307.sock
default-character-set=utf8

[mysqld]
port            = 3307
socket          = /mydata/data/3307/mysql_3307.sock
pid-file        = /mydata/data/3307/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3307
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
lower_case_table_names=1
max_connections=1000

5、修改数据目录的属主、属组
[root@CentOS /]# chown -R mysql:mysql /mydata/


6、添加环境变量
[root@CentOS bin]# vi /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
[root@CentOS etc]# source /etc/profile


7、初始化
[root@CentOS /]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3306 --user=mysql
[root@CentOS /]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3307 --user=mysql
[root@CentOS /]# chown -R mysql:mysql /mydata/


8、启动/关闭 实例

[root@CentOS 3307]# netstat -tulpn | grep -i mysql
[root@CentOS 3307]# mysqld_safe  --defaults-file=/mydata/data/3306/my.cnf 2>&1 > /dev/null &
[1] 2026
[root@CentOS 3307]# mysqld_safe  --defaults-file=/mydata/data/3307/my.cnf 2>&1 > /dev/null &
[2] 2251
[root@CentOS 3307]# netstat -tulpn | grep -i mysql
tcp        0      0 :::3306                     :::*                        LISTEN      2229/mysqld         
tcp        0      0 :::3307                     :::*                        LISTEN      2454/mysqld 

# 关闭实例,这里mysql的root用户并没有设置密码,密码提示直接敲 Enter即可
[root@CentOS 3306]# mysqladmin -uroot -p -S /mydata/data/3306/mysql_3306.sock shutdown
Enter password: 
[1]-  Done                    mysqld_safe --defaults-file=/mydata/data/3306/my.cnf &>/dev/null  (wd: /mydata/data/3307)
(wd now: /mydata/data/3306)
[root@CentOS 3306]# mysqladmin -uroot -p -S /mydata/data/3307/mysql_3307.sock shutdown
Enter password: 
[2]+  Done                    mysqld_safe --defaults-file=/mydata/data/3307/my.cnf &>/dev/null  (wd: /mydata/data/3307)
(wd now: /mydata/data/3306)
[root@CentOS 3306]# netstat -tulpn | grep -i mysql

9、测试连接登录实例
#我们需要指定通过哪一个套接字进行连接
#注意:这里mysql的root用户并没有设置密码,所以可以直接登录

[root@CentOS /]# mysql -uroot -p -S /mydata/data/3306/mysql_3306.sock
mysql> create database d3306;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| d3306              |
| data               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.02 sec)

mysql> exit;
[root@CentOS /]# mysql -uroot -p -S /mydata/data/3307/mysql_3307.sock
mysql> create database d3307;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| d3307              |
| data               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.01 sec)

mysql> system date
2015年 11月 19日 星期四 05:00:48 CST

参考:

http://www.linuxidc.com/Linux/2014-07/104508.htm

你可能感兴趣的:(CentOS6.5下二进制多实例安装mysql-5.6.27)