mysql80-DBA数据库学习4-多实例

什么是MySQL多实例
MySQL的多实例就是在一台机器上开启多个不同的服务端口,运行多个MySQL服务进程,使用不同的socket来监听这多个不同的端口以此提供服务,这一点和Oracle的多实例类似。这些MySQL的实例共用相同的MySQL但是使用的参数文件是不一样的,相应的数据文件也不同。提供服务的时候从逻辑上看各自独立,各自获取的硬件资源可以灵活设定。

MySQL多实例优劣势
有效的利用服务器资源。当单个服务器的资源有剩余的时候可以将多余的资源有效的利用起来,而还实现了资源的逻辑隔离。
节约经济消耗。例如需要多个数据库来搭建主从,但是又只有一台服务器。
当单个数据库并发很高或计算资源需求很高时。整个实例会消耗大量系统的CPU,IO等资源。这样其他实例的可利用资源就会变少产生问题。无法实现实例资源的绝对隔离。
如何部署MySQL多实例
部署的方式有两种:

1.使用mysqld_multi工具,用单独的配置文件实现多实例配置复杂但是管理方便。

​2.设置多个配置文件启动,这样启动不同进程实现多实例。原理简单,但是不易管理。

源码下载

MySQL :: Download MySQL Community Server (Archived Versions)

-rw-r--r--. 1 root root 761885581 Mar 27 16:09 mysql-5.7.44-el7-x86_64.tar.gz
-rw-r--r--. 1 root root 440898798 Mar 27 15:54 mysql-8.3.0.tar.gz

解压软件至/usr/local/目录下 (目录可以自定)

tar xf mysql-5.7.44-el7-x86_64.tar.gz -C /usr/local/

tar xvf mysql-5.7.44-el7-x86_64.tar.gz -C /usr/local/          注xvf会看到解压的信息。

[root@xntz1 ~]# tar xf mysql-5.7.44-el7-x86_64.tar.gz -C /usr/local/
[root@xntz1 ~]# cd /usr/local
[root@xntz1 local]# ls
bmysql-5.7.44-el7-x86_64
[root@xntz1 local]# 

创建用户和组

groupadd -r mysql
useradd -M -s /sbin/nologin -g mysql mysql
[root@xntz1 local]# useradd
Options:

  -M, --no-create-home          do not create the user's home directory
  -r, --system                  create a system account
  -s, --shell SHELL             login shell of the new account

目录配置

[root@xntz1 local]# mv mysql-5.7.44-el7-x86_64 mysql
[root@xntz1 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@xntz1 local]# pwd
/usr/local

不用mv也可以用ln -s mysql-5.7.44-el7-x86_64 mysql进行软连接。

修改目录/usr/local/mysql的属主属组
 

[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll mysql -d
drwxr-xr-x. 9 mysql mysql 129 Jul  2 13:38 mysql

//配置环境变量

[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 
[root@localhost local]# which mysql
/usr/local/mysql/bin/mysql

创建数据存放目录(多个)并修改属主属组

[root@xntz1 /]# mkdir /mysqldata
[root@xntz1 /]# cd mysqldata
[root@xntz1 mysqldata]# mkdir -p /mysqldata/{3306,3307,3308}

[root@xntz1 mysqldata]# ls /mysqldata
3306  3307  3308
[root@xntz1 mysqldata]# chown -R mysql:mysql /mysqldata

[root@xntz1 /]# ll / | grep mysql*
drwxr-xr-x.   5 mysql mysql   42 Mar 28 09:23 mysqldata
[root@xntz1 /]# 

[root@xntz1 /]# ll /mysqldata/
total 0
drwxr-xr-x. 2 mysql mysql 6 Mar 28 09:23 3306
drwxr-xr-x. 2 mysql mysql 6 Mar 28 09:23 3307
drwxr-xr-x. 2 mysql mysql 6 Mar 28 09:23 3308

初始化各实例

mysqld --initialize --datadir=/mysqldata/3306 --user=mysql

[root@xntz1 mysqldata]# mysqld --initialize --datadir=/mysqldata/3306 --user=mysql

2024-03-28T02:40:43.461774Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-03-28T02:40:44.928589Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-03-28T02:40:45.051767Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-03-28T02:40:45.115782Z 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: 93ac9e08-ecac-11ee-8abc-005056a37663.
2024-03-28T02:40:45.117844Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-03-28T02:40:48.712845Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-03-28T02:40:48.712881Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-03-28T02:40:48.717177Z 0 [Warning] CA certificate ca.pem is self signed.
2024-03-28T02:40:49.360958Z 1 [Note] A temporary password is generated for root@localhost: ile/j.=_K7TA

[root@xntz1 mysqldata]# echo ' ile/j.=_K7TA'>/mysqldata/3306pass.txt  保存密码

--initialize 会在日志里打印出一个随机密码。
--initialize-insecure 不会产生随机密码,第一次登陆数据库使用空密码。

例如:mysqld --initialize-insecure --datadir=/mysqldata/3306 --user=mysql 那么密码就会为空。

mysqld --initialize --datadir=/mysqldata/3307 --user=mysql

mysqld --initialize --datadir=/mysqldata/3308 --user=mysql

[root@xntz1 mysqldata]# mysqld --initialize --datadir=/mysqldata/3307 --user=mysql
2024-03-28T02:45:18.122523Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
******
2024-03-28T02:45:23.427859Z 1 [Note] A temporary password is generated for root@localhost: eqcZurw75e!=

[root@xntz1 mysqldata]# mysqld --initialize --datadir=/mysqldata/3308 --user=mysql
2024-03-28T02:46:48.924633Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
******
2024-03-28T02:46:51.375296Z 1 [Note] A temporary password is generated for root@localhost: 1PCowCry/sy&

echo 'eqcZurw75e!='>/mysqldata/3307pass.txt

echo '1PCowCry/sy&'>/mysqldata/3308pass.txt

安装其他依赖包perl 、ncurses

ncurses是字符终端下屏幕控制的基本库,你在TTY下登录到主机上mysql需要的,所以依赖

Perl是 Practical Extraction and Report Language的缩写,可翻译为 “实用报表提取语言”。

yum安装

配置配置文件

[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin

[mysqld3306]
datadir = /mysqldata/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /mysqldata/3306/mysql_3306.pid
log-error=/var/log/3306.log

[mysqld3307]
datadir = /mysqldata/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /mysqldata/3307/mysql_3307.pid
log-error=/var/log/3307.log

[mysqld3308]
datadir = /mysqldata/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /mysqldata/3308/mysql_3308.pid
log-error=/var/log/3308.log

[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
LISTEN  0       128               [::]:22             [::]:*              
[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308

[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
LISTEN  0       80                   *:3306              *:*              
LISTEN  0       80                   *:3307              *:*              
LISTEN  0       80                   *:3308              *:*              
LISTEN  0       128               [::]:22             [::]:*
————————————————

修改密码

[root@xntz1 mysqldata]# cat 3306pass.txt 
 ile/j.=_K7TA

[root@xntz1 mysqldata]# mysql -uroot -p'ile/j.=_K7TA' -S /tmp/mysql3306.sock
Server version: 5.7.44
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('Root@098');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql>

新密码登录
[root@xntz1 mysqldata]# mysql -uroot -p'Root@098' -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

[root@xntz1 mysqldata]# mysql -uroot -p'Root@098' 
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@xntz1 mysqldata]# 

注意:不使用-S /tmp/mysql3306.sock 是会报错的,默认会加载/tmp/mysql.sock,而没有此文件。

设置开机自启

方法1.# 3306 的开机自启
# 复制一份,在里面修改
 

[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/my3306.service

[root@localhost ~]# cat /usr/lib/systemd/system/my3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
ExecStop=ps -ef|grep 3306|grep -v grep|awk '{print $2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost ~]#

# 设置3306开机自启、并立马启动
 

[root@localhost ~]# systemctl enable --now my3306

[root@localhost ~]# systemctl status my3306

MySQL
启动流程

wKioL1hLzdixcXWmAABaFHN0V18168.png

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都是通过这个接口文件

vim /etc/init.d/mysqld_multi.server

basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
export PATH=/usr/local/mysql/bin:$PATH

  • 28
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值