(一)MysqL多实例概述
实例是进程与内存的一个概述,所谓MysqL多实例,就是在服务器上启动多个相同的MysqL进程,运行在不同的端口(如3306,3307,3308),通过不同的端口对外提供服务。
由于MysqL在一个实例下面可以创建多个数据库,所以通常在一台服务器上只要安装一个MysqL实例即可满足使用。但在实际使用中,因为服务器硬件资源充足,或者业务需要(比如在一台服务器上创建开发数据库和测试数据库),往往会在一台服务器上创建多个实例。
MysqL多实例部署主要有以下两种方式:
- 使用官方自带的MysqLd_multi来配置管理,特点是使用同一份MysqL配置文件,这种方式属于集中式管理,管理起来较为方便;
- 使用单独的MysqL配置文件来单独配置实例,这种方式逻辑简单,数据库之间没有关联。
本文将对第一种方式进行环境搭建学习。
(三)实验环境
操作系统 :CentOS Linux release 7.4.1708 (Core)
数据库版本:5.7.24-log
预计划安装4个MysqL实例,规划信息为:
实例1 | 实例2 | 实例3 | 实例4 |
basedir=/usr/local/MysqL datadir=/MysqL/3306/data port=3306 socket=/tmp/MysqL_3306.sock |
basedir=/usr/local/MysqL datadir=/MysqL/3307/data port=3307 socket=/tmp/MysqL_3307.sock |
basedir=/usr/local/MysqL datadir=/MysqL/3308/data port=3308 socket=/tmp/MysqL_3308.sock |
basedir=/usr/local/MysqL datadir=/MysqL/3309/data port=3309 socket=/tmp/MysqL_3309.sock |
(四)实验过程
(4.1)在安装MysqL之前,需要卸载服务器自带的MysqL包和MysqL数据库分支mariadb的包
[root@masterdb ~]# rpm -qa|grep MysqL [root@masterdb ~]# rpm -qa | mariadb mariadb-libs-5.5.56-2.el7.x86_64 [root@masterdb ~]# rpm -e mariadb-libs-2.el7.x86_64 --nodeps
(4.2)依赖包安装
MysqL对libaio 库有依赖性。如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败 、
对于MysqL 5.7.19和更高版本:通用Linux版本中增加了对非统一内存访问(NUMA)的支持,该版本现在对libnuma库具有依赖性 。
(4.4)解压安装包
[root@masterdb ~]# cd /usr/local/ [root@masterdb local]# tar xzvf /root/MysqL-5.7.24-linux-glibc2.12-x86_64.tar.gz # 修改解压文件名,与前面定义的basedir相同 [root@masterdb local]# mv MysqL-12-x86_64/ MysqL
最终解压结果如下:
[root@masterdb MysqL]# ls -l total 36 drwxr-xr-x 2 root root 4096 Mar 28 13:48 bin -rw-r--r-- 1 7161 31415 17987 Oct 4 2018 COPYING drwxr-xr-x 2 root root 55 Mar docs drwxr-xr-x 3 root root include drwxr-xr-x 5 root root 230 Mar lib drwxr-xr-x 4 root root 30 Mar 48 man -rw-r--r-- 31415 2478 Oct README drwxr-xr-x 28 root root share drwxr-xr-x 90 Mar 48 support-files
(4.5)创建数据文件存放路径
[root@masterdb MysqL]# vim /etc/my.cnf [MysqLd] user=MysqL basedir = /usr/local/MysqL [MysqLd_multi] MysqLd=/usr/local/MysqL/bin/MysqLd_safe MysqLadmin=/usr/local/MysqL/bin/MysqLadmin log=/usr/local/MysqL/MysqLd_multi.log [MysqLd3306] MysqLd=MysqLd MysqLadmin=MysqLadmin datadir=/MysqL/3306/data port= server_id= socket=/tmp/MysqL_3306.sock log-error = /MysqL/error_3306.log [MysqLd3307] MysqLd=3307/MysqL_3307.sock log-error=/MysqL/error_3307.log [MysqLd3308] MysqLd=3308/MysqL_3308.sock log-error=/MysqL/error_3308.log [MysqLd3309] MysqLd=3309/MysqL_3309.sock log-error = /MysqL/3309/error_3309.log
(4.7)初始化数据库
注意,初始化实例的最后一行记录了root的初始密码
# 初始化3306实例 [root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-file=/etc/my.cnf --initialize --basedir=/usr/local/MysqL/ --datadir=/MysqL/data 2020-03-28T06:10:28.484174Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 28.689102Z 0 [Warning] InnoDB: New log files created,LSN=45790 28.723881Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 28.781205Z 0 [Warning] No existing UUID has been found,so we assume that this is the first time that this server has been started. Generating a new UUID: d29ad574-70ba-11ea-a38f-000c29fb6200. 28.782195Z 0 [Warning] Gtid table is not ready to be used. Table 'MysqL.gtid_executed' cannot be opened. 28.783078Z 1 [Note] A temporary password is generated for root@localhost: YuJ6Bi=PtqCJ # 初始化3307实例 [root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-45.598676Z 45.793277Z 45.829673Z 45.886255Z time that this server has been started. Generating a new UUID: dcccdb2f-70ba-11ea-a565-45.887571Z 45.890477Z for root@localhost: &s)nYg.e4qx# [root@masterdb MysqL]# # 初始化3308实例 [root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-55.237714Z 55.442794Z 55.479012Z 55.534839Z time that this server has been started. Generating a new UUID: e28d1d57-70ba-11ea-a5c4-55.535622Z 55.536387Z for root@localhost: Mz<kr!vsh1yj [root@masterdb MysqL]# # 初始化3309实例 [root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-11:05.644331Z 05.840498Z 05.879941Z 05.936262Z time that this server has been started. Generating a new UUID: e8c03ed2-70ba-11ea-a8fb-05.937179Z 05.937877Z for root@localhost: K.KLa30i-sv3
(4.8)设置环境变量
添加了环境变量,操作系统才能够自己找到MysqL、MysqLd_multi等命令的位置
(4.9)使用MysqLd_multi管理多实例
使用MysqLd_multi关闭实例较为麻烦,需要配置密码,因此如何关闭各个实例,见后面章节:(六)关闭多实例数据库 。
(五)访问多实例数据库
在安装完成并启动数据库后,需要去访问各个MysqL实例,这里非常有意思,经常会发现无法连接到数据库上,我们不妨看一下几种连接方式:
连接方式一:使用服务器IP地址,无法连接。这里还是比较好理解的,MysqL创建完成后,数据库账号root@localhost只允许本地连接,参数“-h”后面用服务器IP被认为了远程连接,因此无法登陆
[root@masterdb MysqL]# MysqL -uoot -p -h192.168.10.11 -P3306 Enter password: ERROR 1130 (HY000): Host masterdb' is not allowed to connect to this MysqL server
连接方式二:使用localhost访问数据库,无法连接。我觉得有些匪夷所思,可以看到,MysqL实例使用的socket文件不对
[root@masterdb MysqL]# MysqL -uroot -p -hlocalhost -2002 (HY000): Cant connect to local MysqL server through socket '/tmp/MysqL.sock (2)
连接方式三:使用127.0.0.1访问数据库,可以连接。有些难以理解,理论上127.0.0.1和localhost是对应的,127.0.0.1可以访问数据库,但是localhost却无法访问
[root@masterdb MysqL]# MysqL -uroot -p -h127.0.0.1 -P3306 Enter password: Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 7 Server version: 24 MysqL Community Server (GPL) Copyright (c) 2000,1)">2018,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> exit Bye
连接方式四:使用socket文件连接,可以正常访问
[root@masterdb MysqL]# MysqL -S /tmp/MysqL_3306.sock -p Enter password: Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection 4 Copyright (c) the current input statement. MysqL>
(5.2)修改数据库root@localhost密码
初次登陆MysqL数据库,需要修改root密码,否则无法正常使用
[root@masterdb MysqL]# MysqL -S /tmp/MysqL_3306.sock -p Enter password: Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 45.7.24 Type 'help;' or \hfor help. Type \cto clear the current input statement. -- 无法查询 MysqL> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 修改root@localhost用户的密码 MysqL> alter user root@localhost identified by 123456; Query OK,0 rows affected (0.00 sec) MysqL> flush privilegesexit Bye
使用MysqLd_multi关闭多实例数据库目前来看比较麻烦,需要在my.cnf文件的[MysqLd_multi]模块里面配置用户密码,并且各个数据库的用户密码都需要相同,否则无法关闭。
我们可以看一下使用MysqLd_multi来关闭数据库实例的日志:
# 其它都是错误的,因此3306关闭成功,而其它端口的实例因为密码错误而连接数据库失败,自然没有关闭数据库 Stopping MysqL servers MysqLadmin: [Warning] Using a password on the command line interface can be insecure. MysqLadmin: [Warning] Using a password on the command line interface can be insecure. MysqLadmin: connect to server at localhost Failed error: Access denied for user 'root@'localhost (using password: YES) MysqLadmin: [Warning] Using a password on the command line interface can be insecure. MysqLadmin: connect to server at MysqLd_multi log 21 2020# 结果:仅仅关闭了密码正确的3306端口数据库 Reporting MysqL servers MysqL server from group: MysqLd3306 is not running MysqL server from group: MysqLd3307 is running MysqL server from group: MysqLd3308 is running MysqL server from group: MysqLd3309 is running MysqLd_multi log 58:07 2020
既然知道了MysqLd_multi是调用MysqLadmin来关闭数据库的,那最好的办法还是直接使用MysqLadmin来关闭各个数据库了,下面演示使用MysqLadmin来关闭数据库实例。
【结束】
相关文档集合: |