文章目录
1. 二进制格式mysql安装
下载二进制格式的mysql软件包
[root@longnian ~]# cd /usr/src/
[root@longnian src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
--2020-06-12 16:07:25-- https://downloads.mysql.com/archives/get/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz’
100%[=====================================>] 643,790,848 2.46MB/s in 4m 20s
2020-06-12 16:09:10 (2.36 MB/s) - ‘mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
创建用户和组
[root@longnian src]# groupadd -r mysql
[root@longnian src]# useradd -M -s /sbin/nologin -g mysql mysql
解压软件至/usr/local/
[root@longnian src]# ls
debug kernels mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@longnian src]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@longnian~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.24-linux-glibc2.12-x86_64 share
[root@longnian ~]# cd /usr/local/
[root@longnian local]# ln -sv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.24-linux-glibc2.12-x86_64/’
[root@longnian local]# ll
总用量 4
drwxr-xr-x. 2 root root 6 8月 12 2015 bin
drwxr-xr-x. 2 root root 6 8月 12 2015 etc
drwxr-xr-x. 2 root root 6 8月 12 2015 games
drwxr-xr-x. 2 root root 6 8月 12 2015 include
drwxr-xr-x. 2 root root 6 8月 12 2015 lib
drwxr-xr-x. 2 root root 6 8月 12 2015 lib64
drwxr-xr-x. 2 root root 6 8月 12 2015 libexec
lrwxrwxrwx 1 mysql mysql 35 5月 16 00:35 mysql -> mysql-5.7.24-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 120 5月 16 00:34 mysql-5.7.24-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 13 5月 16 00:39 password
drwxr-xr-x. 2 root root 6 8月 12 2015 sbin
drwxr-xr-x. 5 root root 46 4月 26 19:28 share
drwxr-xr-x. 2 root root 6 8月 12 2015 src
修改目录/usr/local/mysql的属主属组
[root@longnian ~]# chown -R mysql.mysql /usr/local/mysql
[root@longnian ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.24-linux-glibc2.12-x86_64/
添加环境变量
[root@longnian ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@longnian ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@longnian ~]# . /etc/profile.d/mysql.sh
[root@longnian ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@longnian mysql]# mkdir /opt/data
[root@longnian mysql]# chown -R mysql.mysql /opt/data/
[root@longnian mysql]# ll /opt/
总用量 4
drwxr-xr-x 6 mysql mysql 4096 6月 11 02:51 data
初始化数据库
[root@longnian ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2020-06-12T16:18:16.038920Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-12T16:18:16.038920Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-12T16:18:16.038920Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-12T16:18:16.038920Z 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: e8600890-a060-11e8-b1a2-000c294c50b4.
2020-06-12T16:18:16.038920Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-12T16:18:16.038920Z 1 [Note] A temporary password is generatedfor root@localhost: ?TtlHBIyu8bX
// ?TtlHBIyu8bX 是临时密码,是随机的,一定要记下来
生成配置文件
[root@longnian ~]# 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@longnian ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
启动mysql
[root@longnian ~]# service mysqld start
Starting MySQL SUCCESS!
[root@longnian ~]# ps -ef|grep mysql
root 2395 1 0 00:23 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 2578 2395 9 00:23 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=longnian.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 2608 2331 0 00:23 pts/1 00:00:00 grep --color=auto mysql
[root@longnian ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:20048 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:3260 *:*
LISTEN 0 128 *:55840 *:*
LISTEN 0 64 *:2049 *:*
LISTEN 0 64 *:33831 *:*
LISTEN 0 64 :::52936 :::*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::20048 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::49434 :::*
LISTEN 0 128 :::3260 :::*
LISTEN 0 64 :::2049 :::*
使用临时密码登录
[root@longnian ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)
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('longnian123.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
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...]
常用的选项:
-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
3.3 mysql数据恢复
备份整个数据库(全备)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use longnian;
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_longnian |
+--------------------+
| student |
| style |
+--------------------+
2 rows in set (0.00 sec)
[root@longnian ~]# ls
anaconda-ks.cfg cerfitication.sh
[root@longnian ~]# mysqldump -uroot -p --all-databases > all-202006121657.sql
Enter password:
[root@longnian ~]# ls
all-202006121657.sql anaconda-ks.cfg cerfitication.sh
备份longnian库的student表和style表
[root@longnian ~]# mysqldump -uroot -p --databases longnian > table-202006121701.sql
Enter password:
[root@longnian ~]# ls
all-202006121657.sql anaconda-ks.cfg cerfitication.sh table-202006121701.sql
备份longnian库
[root@longnian ~]# mysqldump -uroot -p --databases longnian > ln-202006121702.sql
Enter password:
[root@longnian ~]# ls
all-202006121657.sql cerfitication.sh table-202006121701.sql
anaconda-ks.cfg ln-202006121702.sql
模拟误删longnian数据库
mysql> drop database longnian;
Query OK, 2 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
恢复longnian数据库
[root@longnian ~]# ls
all-202006121657.sql cerfitication.sh table-202006121701.sql
anaconda-ks.cfg ln-202006121702.sql
[root@longnian ~]# mysql -uroot -p < all-202006121657.sql
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
恢复longnian库的student表和style表
mysql> show tables;
+--------------------+
| Tables_in_longnian |
+--------------------+
| student |
| style |
+--------------------+
2 rows in set (0.00 sec)
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
mysql> drop table style;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> source table-202006121701.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)
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, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
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.01 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, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
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)
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, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
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, 1 warning (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.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+--------------------+
| Tables_in_longnian |
+--------------------+
| student |
| style |
+--------------------+
2 rows in set (0.00 sec)
模拟删除整个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database longnian;
Query OK, 2 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
恢复整个数据库
[root@longnian ~]# ls
all-202006121657.sql cerfitication.sh table-202006121701.sql
anaconda-ks.cfg ln-202006121702.sql
[root@longnian ~]# mysql -uroot -p < all-202006121657.sql
Enter password:
[root@longnian ~]# mysql -uroot -p -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
3.4 差异备份与恢复
3.4.1. mysql差异备份
开启MySQL服务器的二进制日志功能
[root@longnian ~]# 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@longnian ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
对数据库进行完全备份
[root@longnian ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log MySQL Community Server (GPL)
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables from longnian;
+--------------------+
| Tables_in_longnian |
+--------------------+
| student |
| style |
+--------------------+
2 rows in set (0.00 sec)
mysql> select * from longnian.style;
+----+-------------+
| Id | Name |
+----+-------------+
| 1 | linjunjie |
| 2 | zhouejielun |
+----+-------------+
2 rows in set (0.00 sec)
mysql> select * from longnian.student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)
[root@longnian ~]# mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-202006121722.sql
Enter password:
[root@longnian ~]# ll
总用量 804
-rw-r--r-- 1 root root 805731 6月 13 01:14 all-202006121722.sql
-rw-------. 1 root root 954 4月 26 19:31 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 1701 5月 13 20:25 cerfitication.sh
-rw-r--r-- 1 root root 2892 6月 13 00:55 ln-202006121702.sql
-rw-r--r-- 1 root root 2892 6月 13 00:53 table-202006121701.sql
[root@longnian ~]# ls
all-202006121722.sql cerfitication.sh table-202006121701.sql
anaconda-ks.cfg ln-202006121702.sql
对数据库进行修改
mysql> use longnian;
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 student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)
mysql> update student set age = 40 where id = 7;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 40 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)
3.4.2. mysql差异备份恢复
模拟误删数据
mysql> drop database longnian;
Query OK, 2 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
刷新创建新的二进制日志
[root@longnian ~]# ll /opt/data/
总用量 122952
-rw-r----- 1 mysql mysql 56 5月 16 00:39 auto.cnf
-rw-r----- 1 mysql mysql 1007 6月 13 01:10 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 6月 13 01:18 ibdata1
-rw-r----- 1 mysql mysql 50331648 6月 13 01:18 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 5月 16 00:39 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 6月 13 01:14 ibtmp1
-rw-r----- 1 mysql mysql 22184 6月 13 01:10 longnian.localdomain.err
drwxr-x--- 2 mysql mysql 4096 6月 13 01:04 mysql
-rw-r----- 1 mysql mysql 611 6月 13 01:18 mysql.000002
-rw-r----- 1 mysql mysql 15 6月 13 01:14 mysql.index
-rw-r----- 1 mysql mysql 5 6月 13 01:10 mysql.pid
drwxr-x--- 2 mysql mysql 8192 5月 16 00:39 performance_schema
drwxr-x--- 2 mysql mysql 8192 5月 16 00:39 sys
[root@longnian ~]# mysqladmin -uroot -p flush-logs
Enter password:
[root@longnian ~]# ll /opt/data/
总用量 122956
-rw-r----- 1 mysql mysql 56 5月 16 00:39 auto.cnf
-rw-r----- 1 mysql mysql 1007 6月 13 01:10 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 6月 13 01:18 ibdata1
-rw-r----- 1 mysql mysql 50331648 6月 13 01:18 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 5月 16 00:39 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 6月 13 01:14 ibtmp1
-rw-r----- 1 mysql mysql 22285 6月 13 01:19 longnian.localdomain.err
drwxr-x--- 2 mysql mysql 4096 6月 13 01:04 mysql
-rw-r----- 1 mysql mysql 654 6月 13 01:19 mysql.000002
-rw-r----- 1 mysql mysql 154 6月 13 01:19 mysql.000003
-rw-r----- 1 mysql mysql 30 6月 13 01:19 mysql.index
-rw-r----- 1 mysql mysql 5 6月 13 01:10 mysql.pid
drwxr-x--- 2 mysql mysql 8192 5月 16 00:39 performance_schema
drwxr-x--- 2 mysql mysql 8192 5月 16 00:39 sys
恢复完全备份
[root@longnian ~]# mysql -uroot -p < all-202006121722.sql
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| longnian |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use longnian;
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_longnian |
+--------------------+
| student |
| style |
+--------------------+
2 rows in set (0.00 sec)