MySQL 多实例部署
1.下载安装包
[root@localhost ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
2.解压至/usr/local
[root@localhost ~]# tar -xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.35-linux-glibc2.12-x86_64 sbin share src
3.做mysql目录的软连接,并更改其属主属组
[root@localhost local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.35-linux-glibc2.12-x86_64/"
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
lrwxrwxrwx. 1 root root 36 8月 27 12:42 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 8月 27 12:41 mysql-5.7.35-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 4月 21 19:36 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll -d mysql
lrwxrwxrwx. 1 mysql mysql 36 8月 29 05:50 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
4.环境变量
[root@localhost local]# vim /etc/profile.d/mysql.sh
[root@localhost local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# which mysql
/usr/local/mysql/bin/mysql
5.创建各实例存放的目录
[root@localhost ]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ]# ls /opt/data/
3306 3307 3308
6.初始化各个实例
3306
[root@localhost profile.d]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2021-08-29T10:22:56.524466Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-29T10:22:56.692402Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-29T10:22:56.721919Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-29T10:22:56.777888Z 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: 13e7f1c7-08b3-11ec-aed0-000c2988823d.
2021-08-29T10:22:56.778394Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-29T10:22:57.784376Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T10:22:57.784497Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T10:22:57.784999Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-29T10:22:57.842963Z 1 [Note] A temporary password is generated for root@localhost: ;ahco8p#)#L,
//保存临时密码
[root@localhost data]# echo ';ahco8p#)#L,' > 06passwd
3307
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2021-08-27T12:10:22.121228Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-27T12:10:23.371094Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-27T12:10:23.551015Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-27T12:10:23.988171Z 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: c1eaa7e8-072f-11ec-8eee-000c2978b180.
2021-08-27T12:10:23.991268Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-27T12:10:25.006022Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-27T12:10:25.006044Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-27T12:10:25.014096Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-27T12:10:25.139118Z 1 [Note] A temporary password is generated for root@localhost: OpCs&?ILf0z)
//保存临时密码
[root@localhost ~]# echo 'OpCs&?ILf0z)' > 3307_passwd
3308
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2021-08-27T12:11:08.680649Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-27T12:11:10.064525Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-27T12:11:10.267492Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-27T12:11:10.356165Z 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: dd8ddb31-072f-11ec-9185-000c2978b180.
2021-08-27T12:11:10.357696Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-27T12:11:11.694578Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-27T12:11:11.694596Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-27T12:11:11.695690Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-27T12:11:12.272690Z 1 [Note] A temporary password is generated for root@localhost: nGpdhZUu;48q
//存储临时密码
[root@loecho 'nGpdhZUu;48q' > 3308_passwd
查看存储的三个临时密码
[root@localhost ~]# ls
3306_passwd 3308_passwd mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
3307_passwd anaconda-ks.cfg
7.安装perl,查看依赖包是否安装
[root@localhost ~]# yum -y install install perl
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.cn99.com
[root@localhost ~]# ldd /usr/local/mysql/bin/mysql
linux-vdso.so.1 => (0x00007ffecdbfb000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f543b00b000)
librt.so.1 => /lib64/librt.so.1 (0x00007f543ae03000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f543abff000)
libncurses.so.5 => /lib64/libncurses.so.5 (0x00007f543a9d8000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f543a6d0000)
libm.so.6 => /lib64/libm.so.6 (0x00007f543a3ce000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f543a1b8000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5439deb000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f5439bc1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f543b227000
8.编辑/etc/my.cnf配置文件
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/3306.sock
pid-file = /opt/date/3306/mysql.pid
log-error = /var/log/mysql_3306.log
[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/3307.sock
pid-file = /opt/date/3307/mysql.pid
log-error = /var/log/mysql_3307.log
[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/3308.sock
pid-file = /opt/date/3308/mysql.pid
log-error = /var/log/mysql_3308.log
9.更改密码、
[root@localhost ~]# cat 3306_pass
cc6s;of>kf)
[root@localhost ~]# mysql -uroot -p'cc6s;of>kf)' -h127.0.0.1 -h3306
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.22
Copyright (c) 2000, 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> set password = password('1');
Query OK, 0 rows affected, 1 warning (0.01 sec)
[root@localhost ~]# cat 3307_pass
Cdp5'dmz;ao
[root@localhost ~]# mysql -uroot -pCdp5'dmz;ao -h127.0.0.1 -p3307
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.22
Copyright (c) 2000, 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> set password = password('2');
Query OK, 0 rows affected, 1 warning (0.01 sec)
[root@localhost ~]# cat 3308_pass
3d5fl)f9rtf
[root@localhost ~]# mysql -uroot -p'3d5fl)f9rtf' -h127.0.0.1 -P3308
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 2
Server version: 5.7.35
Copyright (c) 2000, 2021, 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> set password = password('3');
Query OK, 0 rows affected, 1 warning (0.00 sec)
10.设置开机自启
//将服务文件拷贝到init.d下,并重命名为mysql
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
//
[root@localhost ~]# ECHO export PATH=/usr/local/mysql/bin:$PATH > /etc/init.d/mysqld
//赋予可执行权限
[root@localhost ~]# chmod +x /etc/init.d/mysqld
//添加服务
[root@localhost ~]# chkconfig --add mysqld
//启动
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
Xtrabackup
Xtrabackup介绍
Xtrabackup是由percona开源的免费数据库热备份软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁);mysqldump备份方式是采用的逻辑备份,其最大的缺陷是备份和恢复速度较慢,如果数据库大于50G,mysqldump备份就不太适合。
Xtrabackup安装完成后有4个可执行文件,其中2个比较重要的备份工具是innobackupex、xtrabackup
1)xtrabackup 是专门用来备份InnoDB表的,和mysql server没有交互;
2)innobackupex 是一个封装xtrabackup的Perl脚本,支持同时备份innodb和myisam,但在对myisam备份时需要加一个全局的读锁。
3)xbcrypt 加密解密备份工具
4)xbstream 流传打包传输工具,类似tar
Xtrabackup优点
1)备份速度快,物理备份可靠
2)备份过程不会打断正在执行的事务(无需锁表)
3)能够基于压缩等功能节约磁盘空间和流量
4)自动备份校验
5)还原速度快
6)可以流传将备份传输到另外一台机器上
7)在不增加服务器负载的情况备份数据
Xtrabackup备份原理
备份开始时首先会开启一个后台检测进程,实时检测mysq redo的变化,一旦发现有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log中,之后复制innodb的数据文件一系统表空间文件ibdatax,复制结束后,将执行flush tables with readlock,然后复制.frm MYI MYD等文件,最后执行unlock tables,最终停止xtrabackup_log
输出如下提示信息
xtrabackup: Transaction log of lsn (2543172) to (2543181) was copied.
171205 10:17:52 completed OK!
Xtrabackup增量备份介绍
xtrabackup增量备份的原理是:
1)、首先完成一个完全备份,并记录下此时检查点LSN;
2)、然后增量备份时,比较表空间中每个页的LSN是否大于上次备份的LSN,若是则备份该页并记录当前检查点的LSN。
增量备份优点:
1)、数据库太大没有足够的空间全量备份,增量备份能有效节省空间,并且效率高;
2)、支持热备份,备份过程不锁表(针对InnoDB而言),不阻塞数据库的读写;
3)、每日备份只产生少量数据,也可采用远程备份,节省本地空间;
4)、备份恢复基于文件操作,降低直接对数据库操作风险;
5)、备份效率更高,恢复效率更高。
实例
下载安装xtrabackup
[root@localhost ~]# yum -y install percona-xtrabackup-2.3.10-1.el7.x86_64.rpm
[root@localhost ~]# ls
anaconda-ks.cfg
percona-x
[root@localhost ~]# yum -y install percona-xtrabackup-2.3.10-1.el7.x86_64.rpm
//进行备份与恢复
[root@localhost ~]# innobackupex --user=root--password=xym /opt/mysqlbackup/full/
MySQL binlog position:filename 'mysql-bin.000004', position '107'
210827 21:03:23 [00]Writing backup-my.cnf
210827 21:03:23 [00] ...done
210827 21:03:23 [00]Writing xtrabackup_info
210827 21:03:23 [00] ...done
xtrabackup: Transactionlog of lsn (1595675) to (1595675) was copied.
210827 21:03:24 completedOK!
//查看全量备份的文件
[root@localhost ~]# ls /opt/mysqlbackup/full
2021-08-29_20-01-06
[root@localhost ~]# ls /opt/mysqlbackup/full/2021-08-29_20-01-06/
backup-my.cnf mysql xtrabackup_binlog_info xtrabackup_info
ibdata1 performance_schema xtrabackup_checkpoints xtrabackup_logfile
//备份数据库的用户需要具有相应权限.
[root@localhost ~]mysql -uroot -pxym
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35
Copyright (c) 2000, 2021, 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> create user 'bkpuser'@'localhost'identified by '123456';
Query OK, 0 rows affected(0.06 sec)
mysql> revoke all privileges,grantoption from 'bkpuser'@'localhost';
Query OK, 0 rows affected(0.00 sec)
mysql> grant reload,lock tables,replication client,process on *.* to 'bkpuser'@'localhost';
Query OK, 0 rows affected(0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected(0.05 sec)
//创建数据库 表 插入数据
mysql> create database xym;
Query OK, 1 row affected(0.00 sec)
mysql> use xym;
Database changed
mysql> create table 1 (id int,name char(16) not null);
Query OK, 0 rows affected(0.05 sec)
mysql> insert into 1 values(1,'caiaochen');
Query OK, 1 row affected(0.02 sec)
mysql> insert into 1 values(2,'xk');
Query OK, 1 row affected(0.02 sec)
mysql> select * from tb1;
+------+----------+
| id | name |
+------+----------+
| 1 | caiaochen |
| 2 | xk |
+------+----------+
2 rows in set (0.00 sec)
mysql> quit
Bye
//增量备份二进制文件
[root@localhost ~]# mysqlbinlog --start-position=107 /usr/local/mysql/data/mysql-bin.000004> /opt/mysqlbackup/inc/`date +%F`.sql
//执行误操作删库
[root@localhost ~]# rm -rf /usr/local/mysql/data/*
//准备全备的redo日志
[root@localhost ~]# innobackupex --apply-log/opt/mysqlbackup/full/2021-08-29_20-01-06/
.........
InnoDB: File './ibtmp1'size is now 12 MB.
InnoDB: 96 redo rollbacksegment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redorollback segment(s) are active.
InnoDB: 5.7.13 started;log sequence number 1595925
xtrabackup: startingshutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimizethread exiting.
InnoDB: Startingshutdown...
InnoDB: Shutdowncompleted; log sequence number 1595944
210827 21:33:32 completedOK!
//使用innobakupex命令的 --copy-back 进行拷贝
[root@localhost ~]# innobackupex --copy-back/opt/mysqlbackup/full/2021-08-29_20-01-06/
//修改属主属组
[root@localhost ~]# chown -R mysql:mysql/usr/local/mysql/data/
//关闭二进制文件
mysql> set sql_log_bin=0;
Query OK, 0 rows affected(0.00 sec)
//读取增量备份文件
mysql> source/opt/mysqlbackup/inc/2021-08-29.sql
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected (0.00sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected(0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| xym |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use xym;
Database changed
mysql> select * from 1;
+------+----------+
| id | name |
+------+----------+
| 1 | caiaochen|
| 2 | xk |
+------+----------+
2 rows in set (0.01 sec)