mysql数据库备份与恢复
mysql数据库备份与恢复
数据库常用备份方案
数据库备份方案:
- 全量备份
- 增量备份
- 差异备份
备份方案 | 特点 |
---|---|
全量备份 | 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。 数据恢复快。 备份时间长 |
增量备份 | 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份 与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件,如此类推。 没有重复的备份数据 备份时间短 恢复数据时必须按一定的顺序进行 |
差异备份 | 备份上一次的完全备份后发生变化的所有文件。 差异备份是指在一次全备份后到进行差异备份的这段时间内 对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。 |
mysql备份工具mysqldump
//语法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
//常用的OPTIONS:
-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
备份整个数据库(全备)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hehe;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_hehe |
+----------------------+
| chenshuo |
| guohui |
| haha |
+----------------------+
3 rows in set (0.00 sec)
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-20180.sql
Enter password:
[root@localhost ~]# ls
all-20180.sql anaconda-ks.cfg
备份hehe库的haha表和guohui表
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 hehe haha guohui > table-20180.sql
Enter password:
[root@localhost ~]# ls
all-20180.sql anaconda-ks.cfg table-20180.sql
备份hehe库
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 --databases hehe > wq-20180.sql
Enter password:
[root@localhost ~]# ls
all-20180.sql table-20180.sql
anaconda-ks.cfg wq-20180.sql
模拟误删hehe数据库
mysql> drop database hehe;
Query OK, 3 rows affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql数据恢复
恢复hehe数据库
[root@localhost ~]# ls
all-20180.sql table-20180.sql
anaconda-ks.cfg wq-20180.sql
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 < wq-20180.sql
Enter password:
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
恢复hehe数据库的haha表和guohui表
mysql> use hehe;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> source table-20180.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.00 sec)
......
......
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------------+
| Tables_in_hehe |
+----------------------+
| chenshuo |
| guohui |
| hehe |
+----------------------+
3 rows in set (0.00 sec)
模拟删除整个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database hehe;
Query OK, 3 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
恢复整个数据库
[root@localhost ~]# ls
all-20180.sql table-20180.sql
anaconda-ks.cfg wq-20180.sql
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 < all-20180.sql
Enter password:
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
差异备份与恢复
mysql差异备份
开启MySQL服务器的二进制日志功能
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
user = mysql
pid-file = /tmp/mysql.pid
skip-name-resolve
server-id=1 //设置服务器标识符
log-bin=mysql_bin //开启二进制日志功能
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
对数据库进行完全备份
[root@localhost ~]# mysql -uroot -p123456
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.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables from hehe;
+--------------------+
| Tables_in_hehe |
+--------------------+
| kk |
| student |
+--------------------+
2 rows in set (0.01 sec)
mysql> select * from hehe.kk;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
+------+-------+------+
2 rows in set (0.01 sec)
mysql> select * from hehe.kk;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangwu | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
完全备份
[root@localhost ~]# mysqldump -uroot -p123456 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-20190.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll
总用量 792
-rw-r--r--. 1 root root 803946 2月 21 13:47 all-20190.sql
-rw-------. 1 root root 1259 1月 7 16:39 anaconda-ks.cfg
增加新内容
[root@localhost ~]# mysql -uroot -p123456
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 4
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> use hehe;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> insert into kk values(3,'hehe',20),(4,'xixi',50);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from kk;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 20 |
| 4 | xixi | 50 |
+------+-------+------+
4 rows in set (0.00 sec)
mysql> update kk set age = 40 where id = 3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from kk;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 40 |
| 4 | xixi | 50 |
+------+-------+------+
4 rows in set (0.00 sec)
mysql差异备份恢复
模拟误删数据
[root@localhost ~]# mysql -uroot -p123456 -e 'drop database hehe;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
由上可以看到hehe这个数据库已被删除
刷新创建新的二进制日志
[root@localhost ~]# ll /opt/data/
总用量 188548
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:09 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:09 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:03 mysql
-rw-r-----. 1 mysql mysql 154 2月 21 16:09 mysql_bin.000001
-rw-r-----. 1 mysql mysql 19 2月 21 16:09 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
刷新创建新的二进制日志
[root@localhost ~]# mysqladmin -uroot -p123456 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /opt/data/
总用量 188552
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:17 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:18 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:16 mysql
-rw-r-----. 1 mysql mysql 945 2月 21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql 154 2月 21 16:18 mysql_bin.000002
-rw-r-----. 1 mysql mysql 38 2月 21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
恢复完全备份
[root@localhost ~]# mysql -uroot -p123456< all-20190.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| hehe |
+--------------------+
[root@localhost ~]# mysql -uroot -p123456 -e 'show tables from haha;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Tables_in_hehe |
+--------------------+
| kk |
| student |
+--------------------+
[root@localhost ~]# mysql -uroot -p123456 -e 'select * from hehe.kk;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
+------+-------+------+
[root@localhost ~]# mysql -uroot -p123456 -e 'select * from hehe.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangwu | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
恢复差异备份
[root@localhost ~]# ll /opt/data/
总用量 189572
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:20 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:20 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:20 mysql
-rw-r-----. 1 mysql mysql 945 2月 21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql 786443 2月 21 16:20 mysql_bin.000002
-rw-r-----. 1 mysql mysql 38 2月 21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
drwxr-x---. 2 mysql mysql 96 2月 21 16:20 wangqing
检查误删数据库的位置在什么地方
[root@localhost ~]# mysql -uroot -p123456
mysql> show binlog events in 'mysql_bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000001 | 123 | Previous_gtids | 1 | 154 | |
| mysql_bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 219 | Query | 1 | 295 | BEGIN |
| mysql_bin.000001 | 295 | Table_map | 1 | 353 | table_id: 330 (hehe.kk) |
| mysql_bin.000001 | 353 | Write_rows | 1 | 410 | table_id: 330 flags: STMT_END_F |
| mysql_bin.000001 | 410 | Xid | 1 | 441 | COMMIT /* xid=2628 */ |
| mysql_bin.000001 | 441 | Anonymous_Gtid | 1 | 506 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 506 | Query | 1 | 582 | BEGIN |
| mysql_bin.000001 | 582 | Table_map | 1 | 640 | table_id: 330 (hehe.kk) |
| mysql_bin.000001 | 640 | Update_rows | 1 | 698 | table_id: 330 flags: STMT_END_F |
| mysql_bin.000001 | 698 | Xid | 1 | 729 | COMMIT /* xid=2630 */ |
| mysql_bin.000001 | 729 | Anonymous_Gtid | 1 | 794 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 794 | Query | 1 | 898 | drop database hehe | //此处就是删除数据库的位置,对应的pos位置是794
| mysql_bin.000001 | 898 | Rotate | 1 | 945 | mysql_bin.000002;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)
使用mysqlbinlog恢复差异备份
[root@localhost ~]# mysqlbinlog --stop-position=794 /opt/data/mysql_bin.000001 |mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456-e 'select * from hehe.rkk;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 40 |
| 4 | xixi | 50 |
+------+-------+------+
使用mysqlbinlog恢复差异备份
根据时间恢复:
mysqlbinlog --stop-datetime='22-07-28 15:42:44' /opt/data/mysql_bin.000001 | mysql -uroot -p123456
根据操作id号恢复:
mysqlbinlog --stop-position=769 /opt/data/mysql_bin.000001 | mysql -uroot -p123456
二进制日志转换文本文件:
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql_bin.000004 > /opt/mysql_bin004.txt
mysql多实例部署
下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]#
创建用户
[root@localhost src]# useradd -M -r -s /sbin/nologin mysql
[root@localhost src]# id mysql
uid=975(mysql) gid=974(mysql) 组=974(mysql)
解压软件至/usr/local/
[root@localhost src]# ls
debug kernels mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.37-linux-glibc2.12-x86_64 sbin share src
[root@localhost src]#
创建软连接
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ll | grep mysql
lrwxrwxrwx. 1 root root 36 7月 26 05:51 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 7月 26 05:50 mysql-5.7.37-linux-glibc2.12-x86_64
[root@localhost local]#
修改目录/usr/local/mysql的属主属组和主包目录的属主属组
[root@localhost local]# cd
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/
[root@localhost local]# ll | grep mysql
lrwxrwxrwx. 1 mysql mysql 36 7月 26 05:51 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 7月 26 05:50 mysql-5.7.37-linux-glibc2.12-x86_64
[root@localhost local]#
添加环境变量
[root@localhost local]# cd mysql
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost mysql]# cat /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
修改头文件
[root@localhost mysql]# ln -s /usr/local/mysql/include/ /usr/local/include/myspl
[root@localhost ~]# cd /usr/local/include/
[root@localhost include]# ll
总用量 0
lrwxrwxrwx. 1 root root 25 7月 26 06:15 myspl -> /usr/local/mysql/include/
[root@localhost include]#
修改man文件路径
[root@localhost ~]# vim /etc/man_db.conf
....
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man
#---------------------------------------------------------
# set up PATH to MANPATH mapping
# ie. what man tree holds man pages for what binary directory.
....
增添额外库文件搜索路径
[root@localhost ~]# cd /etc/ld.so.conf.d/
[root@localhost ld.so.conf.d]# ls
kernel-4.18.0-358.el8.x86_64.conf libiscsi-x86_64.conf
[root@localhost ld.so.conf.d]# vim mysql.conf
[root@localhost ld.so.conf.d]# cat mysql.conf
/usr/local/mysql/bin
[root@localhost ld.so.conf.d]# ldconfig
创建各实例数据存放的目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
总用量 0
drwxr-xr-x 2 mysql mysql 6 7月 28 17:24 3306
drwxr-xr-x 2 mysql mysql 6 7月 28 17:24 3307
drwxr-xr-x 2 mysql mysql 6 7月 28 17:24 3308
初始化各实例
初始化3306实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-28T01:43:33.987463Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-28T01:43:34.565759Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-28T01:43:34.733540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-28T01:43:34.808595Z 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: 061b480e-72c5-11e9-ae30-000c29c0ed3b.
2022-07-28T01:43:34.810131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-28T01:43:34.837499Z 1 [Note] A temporary password is generated for root@localhost: Y=lJV76Rkx,5
[root@localhost ~]# echo 'Y=lJV76Rkx,5' > 3306-1
初始化3307实列
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-28T01:43:33.987463Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-28T01:43:34.565759Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-28T01:43:34.733540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-28T01:43:34.808595Z 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: 061b480e-72c5-11e9-ae30-000c29c0ed3b.
2022-07-28T01:43:34.810131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-28T01:43:34.837499Z 1 [Note] A temporary password is generated for root@localhost: Kht‘kk.2#:6
[root@localhost ~]# echo 'Kht‘kk.2#:6' > 3307-2
初始化3308实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-28T01:43:33.987463Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-28T01:43:34.565759Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-28T01:43:34.733540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-28T01:43:34.808595Z 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: 061b480e-72c5-11e9-ae30-000c29c0ed3b.
2022-07-28T01:43:34.810131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-28T01:43:34.837499Z 1 [Note] A temporary password is generated for root@localhost:
bpG+yqeGN8G:
[root@localhost ~]# echo 'bpG+yqeGN8G:' > 3308-3
安装perl
[root@localhost ~]# yum -y install perl
配置配置文件/etc/my.cnf
[root@localhost ~]# vim /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/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log
[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log
启动各实例
[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
LISTEN 0 128 *:22 *:*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 80 :::3306 :::*
初始化密码
[root@localhost ~]# ls
3306-1 3307-2 3308-3
anaconda-ks.cfg
[root@localhost ~]# cat 3306-1
Y=lJV76Rkx,5
[root@localhost ~]# mysql -uroot -p'Y=lJV76Rkx,5' -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.22
Copyright (c) 2000, 2022, 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('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> quit
Bye
[root@localhost ~]# cat 3307-2
Kht‘kk.2#:6
[root@localhost ~]# mysql -uroot -p'Kht‘kk.2#:6' -S /tmp/mysql3307.sock -e 'set password = password("123456");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# cat 3308-3
bpG+yqeGN8G:
[root@localhost ~]# mysql -uroot -p'bpG+yqeGN8G:' -S /tmp/mysql3308.sock -e 'set password = password("123456");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
把二进制安装的服务命令用systemct命令代替
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysql-1.service
[root@localhost ~]# vim /usr/lib/systemd/system/mysql-1.service
[Unit]
Description=mysql-1 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start mysql-1 //服务名
ExecStop=ps -ef | grep 3306 | grep {print$2} | grep -v grep | xrags kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
//做个软连接
[root@localhost ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
[root@localhost ~]# systemctl start mysql-1 //服务名
[root@localhost ~]# systemctl enable mysql-1