目录
1. 二进制格式mysql安装
//下载二进制格式的mysql软件包
[root@liping ~]# cd /usr/src/
[root@liping src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
//创建用户和组
[root@liping src]# groupadd -r mysql
[root@liping src]# useradd -r -M -s /sbin/nologin -g mysql mysql
[root@liping src]# id mysql
uid=997(mysql) gid=995(mysql) 组=995(mysql)
//解压软件至/usr/local/
[root@liping src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@liping src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 sbin share src
[root@liping src]# cd /usr/local/
[root@liping local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@liping local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
lrwxrwxrwx. 1 root root 36 12月 13 09:04 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 12月 13 09:03 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 10月 11 00:36 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
//修改目录/usr/local/mysql的属主属组
[root@liping local]# chown -R mysql.mysql /usr/local/mysql
[root@liping local]# ll mysql -d
lrwxrwxrwx. 1 mysql mysql 36 12月 13 09:04 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//添加环境变量
[root@liping ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@liping ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@liping ~]# . /etc/profile.d/mysql.sh
[root@liping ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@liping ~]# mkdir /opt/data
[root@liping ~]# chown -R mysql.mysql /opt/data/
[root@liping ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 12月 13 09:09 data
drwxr-xr-x. 2 root root 56 11月 15 14:52 ssl
//初始化数据库
[root@liping ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-12-13T01:11:14.397447Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-13T01:11:15.470602Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-13T01:11:15.606540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-13T01:11:15.675697Z 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: 75eed57a-1d45-11ea-86a8-000c2926a452.
2019-12-13T01:11:15.676729Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-13T01:11:15.678855Z 1 [Note] A temporary password is generated for root@localhost: V4jkus/iy0uk
[root@liping ~]# echo 'V4jkus/iy0uk' > /root/pass
[root@liping ~]# cat /root/pass
V4jkus/iy0uk
//生成配置文件
[root@liping ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@liping ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//配置服务启动脚本
[root@liping ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@liping ~]# sed -ri 's#^(basedir=).*#1/usr/local/mysql#g' /etc/init.d/mysqld
[root@liping ~]# sed -ri 's#^(datadir=).*#1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@liping ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/liping.err'.
. SUCCESS!
[root@liping ~]# ps -ef|grep mysql
root 1542 1 0 09:21 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 1720 1542 7 09:21 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=liping.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 1752 1340 0 09:22 pts/0 00:00:00 grep --color=auto mysql
[root@liping ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 5 :::873 :::*
LISTEN 0 80 :::3306 :::*
//修改密码
//使用临时密码登录
[root@liping ~]# cat pass
V4jkus/iy0uk
[root@liping ~]# mysql -uroot -pV4jkus/iy0uk
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
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>
//设置新密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
2. mysql配置文件
mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
mysql常用配置文件参数:
参数 | 说明 |
---|---|
port = 3306 | 设置监听端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安装路径 |
datadir = /data/mysql | 指定MySQL的数据存放路径 |
pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
user = mysql | 指定MySQL以什么用户的身份提供服务 |
skip-name-resolve | 禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求 |
3. mysql数据库备份与恢复
3.1 数据库常用备份方案
数据库备份方案:
- 全量备份
- 增量备份
- 差异备份
备份方案 | 特点 |
---|---|
全量备份 | 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。数据恢复快。备份时间长 |
增量备份 | 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件,如此类推。没有重复的备份数据。备份时间短。恢复数据时必须按一定的顺序进行 |
差异备份 | 备份上一次的完全备份后发生变化的所有文件。差异备份是指在一次全备份后到进行差异备份的这段时间内对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。 |
3.2 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 |
| liping |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use liping;
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> show tables;
+------------------+
| Tables_in_liping |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
[root@liping ~]# ls
anaconda-ks.cfg test
[root@liping ~]# mysqldump -uroot -p --all-databases > all-20191213.sql
Enter password:
[root@liping ~]# ls
all-20191213.sql anaconda-ks.cfg test
//备份liping库的student表
[root@liping ~]# mysqldump -uroot -p liping student > table-20191213.sql
Enter password:
[root@liping ~]# ls
all-20191213.sql anaconda-ks.cfg table-20191213.sql test
//备份liping库
[root@liping ~]# mysqldump -uroot -p --databases liping > lp-20191213.sql
Enter password:
[root@liping ~]# ls
all-20191213.sql anaconda-ks.cfg lp-20191213.sql table-20191213.sql test
//模拟误删liping数据库
mysql> drop database liping;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.3 mysql数据恢复
//恢复liping数据库
[root@liping ~]# ls
all-20191213.sql anaconda-ks.cfg lp-20191213.sql table-20191213.sql test
[root@liping ~]# mysql -uroot -p < lp-20191213.sql
Enter password:
[root@liping ~]# mysql -uroot -p -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| liping |
| mysql |
| performance_schema |
| sys |
+--------------------+
//模拟误删liping数据库中的student表
mysql> use liping;
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> show tables;
+------------------+
| Tables_in_liping |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
//恢复liping数据库的student表
[root@liping ~]# ls
all-20191213.sql anaconda-ks.cfg lp-20191213.sql table-20191213.sql test
[root@liping ~]# mysql -uroot -p liping < table-20191213.sql
Enter password:
[root@liping ~]# mysql -uroot -p -e 'show tables from liping;'
Enter password:
+------------------+
| Tables_in_liping |
+------------------+
| student |
+------------------+
3.4 差异备份与恢复
3.4.1. mysql差异备份
开启MySQL服务器的二进制日志功能
[root@liping ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id=1 //设置服务器标识符
log-bin=mysql_bin //开启二进制日志功能
[root@liping ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
对数据库进行完全备份
mysql> use liping;
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> select * from liping.student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
4 rows in set (0.00 sec)
//完全备份
[root@liping ~]# mysqldump -uroot -p123456 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all.2019.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@liping ~]# ll
总用量 788
-rw-r--r--. 1 root root 802272 12月 13 10:19 all.2019.sql
-rw-------. 1 root root 1451 10月 11 00:40 anaconda-ks.cfg
drwxr-xr-x. 3 root root 106 12月 13 09:37 test
//增加新内容
mysql> insert into student values(3,'hehe',20),(4,'xixi',50);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | hehe | 20 |
| 4 | xixi | 50 |
+----+-----------+------+
6 rows in set (0.00 sec)
3.4.2. mysql差异备份恢复
模拟误删数据
[root@liping ~]# mysql -uroot -p123456 -e 'drop database liping;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@liping ~]# 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 |
+--------------------+
刷新创建新的二进制日志
[root@liping ~]# ll /opt/data/
总用量 122944
-rw-r-----. 1 mysql mysql 56 12月 13 09:11 auto.cnf
-rw-r-----. 1 mysql mysql 743 12月 13 10:14 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 12月 13 10:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 12月 13 09:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:19 ibtmp1
-rw-r-----. 1 mysql mysql 12790 12月 13 10:14 liping.err
drwxr-x---. 2 mysql mysql 4096 12月 13 09:46 mysql
-rw-r-----. 1 mysql mysql 600 12月 13 10:25 mysql_bin.000002
-rw-r-----. 1 mysql mysql 19 12月 13 10:19 mysql_bin.index
-rw-r-----. 1 mysql mysql 5 12月 13 10:14 mysql.pid
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 sys
//刷新创建新的二进制日志
[root@liping ~]# mysqladmin -uroot -p123456 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@liping ~]# ll /opt/data/
总用量 122948
-rw-r-----. 1 mysql mysql 56 12月 13 09:11 auto.cnf
-rw-r-----. 1 mysql mysql 743 12月 13 10:14 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 12月 13 10:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 12月 13 09:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:19 ibtmp1
-rw-r-----. 1 mysql mysql 12790 12月 13 10:14 liping.err
drwxr-x---. 2 mysql mysql 4096 12月 13 09:46 mysql
-rw-r-----. 1 mysql mysql 647 12月 13 10:27 mysql_bin.000002
-rw-r-----. 1 mysql mysql 154 12月 13 10:27 mysql_bin.000003
-rw-r-----. 1 mysql mysql 38 12月 13 10:27 mysql_bin.index
-rw-r-----. 1 mysql mysql 5 12月 13 10:14 mysql.pid
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 sys
恢复完全备份
[root@liping ~]# mysql -uroot -p123456 < all.2019.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@liping ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| liping |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@liping ~]# mysql -uroot -p123456 -e 'show tables from liping;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+
| Tables_in_liping |
+------------------+
| student |
+------------------+
[root@liping ~]# mysql -uroot -p123456 -e 'select * from liping.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
恢复差异备份
[root@liping ~]# ll /opt/data/
总用量 123968
-rw-r-----. 1 mysql mysql 56 12月 13 09:11 auto.cnf
-rw-r-----. 1 mysql mysql 743 12月 13 10:14 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:29 ibdata1
-rw-r-----. 1 mysql mysql 50331648 12月 13 10:29 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 12月 13 09:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 12月 13 10:19 ibtmp1
drwxr-x---. 2 mysql mysql 58 12月 13 10:29 liping
-rw-r-----. 1 mysql mysql 12790 12月 13 10:14 liping.err
drwxr-x---. 2 mysql mysql 4096 12月 13 10:29 mysql
-rw-r-----. 1 mysql mysql 647 12月 13 10:27 mysql_bin.000002
-rw-r-----. 1 mysql mysql 784548 12月 13 10:29 mysql_bin.000003
-rw-r-----. 1 mysql mysql 38 12月 13 10:27 mysql_bin.index
-rw-r-----. 1 mysql mysql 5 12月 13 10:14 mysql.pid
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 12月 13 09:11 sys
//检查误删数据库的位置在什么地方
mysql> show binlog events in 'mysql_bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql_bin.000002 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000002 | 219 | Query | 1 | 293 | BEGIN |
| mysql_bin.000002 | 293 | Table_map | 1 | 349 | table_id: 110 (liping.student) |
| mysql_bin.000002 | 349 | Write_rows | 1 | 406 | table_id: 110 flags: STMT_END_F |
| mysql_bin.000002 | 406 | Xid | 1 | 437 | COMMIT /* xid=461 */ |
| mysql_bin.000002 | 437 | Anonymous_Gtid | 1 | 502 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000002 | 502 | Query | 1 | 600 | drop database liping |
| mysql_bin.000002 | 600 | Rotate | 1 | 647 | mysql_bin.000003;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
10 rows in set (0.00 sec)
//使用mysqlbinlog恢复差异备份
[root@liping ~]# mysqlbinlog --stop-position=502 /opt/data/mysql_bin.000002 |mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@liping ~]# mysql -uroot -p123456 -e 'select * from liping.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | hehe | 20 |
| 4 | xixi | 50 |
+----+-----------+------+