一、什么是MySQL多实例
简单的说就是在一台机器上开启多个不同的服务端口(例如:3306、3307),运行多个mysql服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供各自的服务。
这些mysql多实例共用一套mysql安装程序,使用不同(也可以相同)的my.cnf配置文件、启动程序、数据文件。在提供服务时,多实例mysql在逻辑上看来是各自独立的,多个实例是根据配置文件中配置的参数来获取服务器相关硬件资源。
二、MySQL常见应用场景
由于公司业务访问量不是很大,服务器的资源基本都是浪费的,这时候很适合使用多实例的应用,如果对SQL语句优化做的比较好,mysql多实例是一个很值得使用的技术,即使并发很大,合理分配系统资源,也不会有太大问题。
三、MySQL多实例常见配置方案
3.1 安装MySQL数据库
具体安装方法,请参见 编译方式安装MySQL数据库 。
3.2 配置多实例
3.2.1 创建多实例目录
[root@mysql-multi ~]# mkdir -p /data/{3306,3307}/data [root@mysql-multi ~]# tree /data /data ├── 3306 │ └── data └── 3307 └── data
3.2.2 将数据目录及临时目录授权mysql用户(在安装mysql之前已经创建mysql用户组和用户)
[root@mysql-multi ~]# chown -R mysql.mysql /data [root@mysql-multi ~]# chmod -R 1777 /tmp [root@mysql-multi ~]# ls -ld /data/{3306,3307}/data drwxr-xr-x. 2 mysql mysql 4096 1月 18 22:40 /data/3306/data drwxr-xr-x. 2 mysql mysql 4096 1月 18 22:40 /data/3307/data
3.2.3 创建配置文件my.cnf
3306端口 |
3307端口 |
[client] [mysql] [mysqld]
lower_case_table_names = 1 server-id = 1 innodb_additional_mem_pool_size = 4M
[mysqld_safe] |
[client] [mysql] [mysqld]
lower_case_table_names = 1 server-id = 3 innodb_additional_mem_pool_size = 4M
[mysqld_safe] |
3.3 初始化数据库表
初始化3306端口数据库 [root@mysql-multi ~]# cd /application/mysql/scripts/ [root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql WARNING: The host 'mysql-multi' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK ......此处省略 初始化3307端口数据库 [root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql WARNING: The host 'mysql-multi' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK ......此处省略
说明:一般情况下初始化数据库表时日志显示两个“OK”即表示初始化成功。
3.4 启动数据库多实例
[root@mysql-multi ~]# /data/3306/mysql start Starting MySQL... [root@mysql-multi ~]# /data/3307/mysql start Starting MySQL... [root@mysql-multi ~]# netstat -lntup|grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 28220/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 26267/mysqld
说明:“mysql”是自己写的启动脚本就不贴出来了,mysql启动和停止的实质为:
mysql数据库启动命令:
# /application/mysql/bin/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
mysql数据库停止命令:
# /application/mysql/bin/mysqladmin -u${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
3.5 配置环境变量
[root@mysql-multi ~]# echo "export PATH=/application/mysql/bin:$PATH" >> /etc/profile [root@mysql-multi ~]# tail -1 /etc/profile export PATH=/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@mysql-multi ~]# . /etc/profile
3.6 登录数据库
启动3306端口数据库
[root@mysql-multi ~]# mysql -S /data/3306/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
启动3307数据库
[root@mysql-multi ~]# mysql -S /data/3307/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
说明:MySQL数据库在初始登录未设置登录用户名和密码,可以在登录数据库之后增加账户信息。至此配置MySQL数据库多实例完成。