新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
一、MySQL多实例简介
MySQL多实例,简单地说,就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307),同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听来自不同的端口来提供服务;
多实例不仅节省物理主机成本,还有效提升了单台物理主机的CPU、磁盘I/O使用效率,而且还可以在多实例之间做部署数据库HA方案。
随着实例数量的增加,就面临统一管理问题,这样我们就需要用MySQL自带的管理程序 mysqld_multi 来进行管理...
二、MySQL启动流程
mysqld_multi #多实例管理程序
mysqld #MySQL最主要的启动方式,里面有很多参数;现在使用多实例就需要用新的mysql_safe 来启动mysql
mysql_safe #实则还是调用mysqld,并且会读取mysqld中的my.cnf配置参数来启动mysql,mysql_safe本身也有很多参数,但是这些参数会优先于my.cnf
my.cnf #mysql的配置文件
my.sock #mysql创建的sock文件,开启、停止、登陆和管理mysql都是通过这个接口文件
三、接下来基于mysql5.5.52版本,安装方法请看MySQL5.5.52编译安装,利用mysqld_multi配置一个多实例
1、停止单实例mysql数据库
10多年的卓尼网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整卓尼建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“卓尼网站设计”,“卓尼网站推广”以来,每个客户项目都认真落实执行。
[root@db01 ~]# /etc/init.d/mysqld stop Shutting down MySQL. SUCCESS!
2、禁止开机自启动
[root@db01 ~]# chkconfig mysqld off [root@db01 ~]# chkconfig --list mysqld mysqld 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭6:关闭
3、创建多实例根目录/data/目录
[root@db01 ~]# mkdir -p /data/{3306,3307}/data
4、拷贝mysqld_multi程序文件
[root@db01 ~]# cp /application/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server
1)修改mysqld_multi.server路径配置
[root@db01 ~]# sed -i 's#basedir=/usr/local/mysql#basedir=/application/mysql#g' /etc/init.d/mysqld_multi.server [root@db01 ~]# sed -i 's#bindir=/usr/local/mysql/bin#bindir=/application/mysql/bin#g' /etc/init.d/mysqld_multi.server
2)添加mysqld_multi用到的/etc/mysqld_multi.cnf配置文件
#这个模板文件可以用命令mysqld_multi --example导出来
[root@db01 ~]# vim /etc/mysqld_multi.cnf [mysqld_multi] mysqld = /application/mysql/bin/mysqld_safe mysqladmin = /application/mysql/bin/mysqladmin #user = multi_admin #password = my_password [mysqld1] socket = /data/3306/mysql.sock port = 3306 pid-file = /data/3306/mysql.pid datadir = /data/3306/data #language = /application/mysql/share/mysql/english user = mysql [mysqld2] socket = /data/3307/mysql.sock port = 3307 pid-file = /data/3307/mysql.pid datadir = /data/3307/data #language = /application/mysql/share/mysql/english user = mysql
5、配置MySQL多实例的文件权限
通过下面的命令授权mysql用户和用户组管理整个多实例的根目录/data
[root@db01 ~]# chown -R mysql.mysql /data
6、初始化MySQL多实例的数据库文件
(1)初始化MySQL数据库
cd /application/mysql/scripts/ <==注意和MySQL5.1的路径不同,MySQL5.1不在MySQL bin路径下了
3306实例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3306/data \
--user=mysql
3307实例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3307/data \
--user=mysql
提示:--basedir=/application/mysql为MySQL的安装路径,--datadir为不同的实例数据目录
操作过程:
[root@db01 ~]# cd /application/mysql/scripts/ 3306实例 [root@db01 scripts]# /application/mysql/scripts/mysql_install_db \ > --basedir=/application/mysql \ > --datadir=/data/3306/data \ > --user=mysql WARNING: The host 'db01' 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... 161117 14:14:14 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46676 ... OK Filling help tables... 161117 14:14:15 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46683 ... OK 如果有两个ok,就表示初始化成功 3307实例 [root@db01 scripts]# /application/mysql/scripts/mysql_install_db \ > --basedir=/application/mysql \ > --datadir=/data/3307/data \ > --user=mysql Installing MySQL system tables... 161117 14:18:20 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46733 ... OK Filling help tables... 161117 14:18:21 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46740 ... OK 如果有两个ok,就表示初始化成功
7、启动多实例:
1)查看数据库状态
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running
2)启动数据库
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start 1,2 [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running
3)查看端口
[root@db01 ~]# ss -nlutp|grep 330 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",47045,10)) tcp LISTEN 0 50 *:3307 *:* users:(("mysqld",47041,10))
8、停止数据库
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf stop 1,2 [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running
9、登陆数据库
1)启动数据库
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start 1,2
2)登录数据库
方法一:指定端口和主机IP,适合远程连接
mysql -uroot -h227.0.0.1 -P3306
方法二:指定socket登陆,适合在本机连接
mysql -S /data/3307/mysql.sock
操作演示
方法一:指定端口和主机IP,适合远程连接
[root@db01 ~]# mysql -uroot -h227.0.0.1 -P3306 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.52 Source distribution Copyright (c) 2000, 2016, 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>
方法二:指定socket登陆,适合在本机连接
[root@db01 ~]# 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.52 Source distribution Copyright (c) 2000, 2016, 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多实例就配置完成啦O(∩_∩)O~~!!!