MySQL的配置文件在/etc/my.cnf
若是想要不输入用户和密码登录,则修改配置文件,或者在当前用户的家目录下创建一个隐藏文件,文件名是.my.cnf,那么MySQL服务的配置文件就有两种,这里就会涉及到MySQL的配置文件的查找顺序:
/etc/my.cnf------> ~/.my.cnf
//MySQL最后查找到的文件最终生效
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将无法正常处理连接请求 |
额外知识
备份的类型可以根据是否需要数据库离线来分类:(取决于业务的需求,而不是备份工具)
//语法:
mysqldump [OPTIONS] database [tables …]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3…]
//常用的OPTIONS:
-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替
//查看数据库
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
//用mysqldump备份整个数据库(全量备份)
[root@localhost ~]# mysqldump --all-databases > all-databases201902211431
[root@localhost ~]# ls
all-databases201902211431 anaconda-ks.cfg
//查看数据库school中的student表
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
+----+-------+------+
//备份数据库school中的student表
[root@localhost ~]# mysqldump school student > table_from_student201902211435
[root@localhost ~]# ls
all-databases201902211431 anaconda-ks.cfg table_from_student201902211435
//备份school数据库
[root@localhost ~]# mysqldump --databases school > database_school201920211737
[root@localhost ~]# ls
all-databases201902211431 anaconda-ks.cfg database_school201920211737 table_from_student201902211435
//模拟删除school数据库
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.23 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> drop database school;
Query OK, 1 row affected (0.03 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# mysql < database_school201920211737
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
+----+-------+------+
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# mysql < all-databases201902211431
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
+----+-------+------+
差异备份用到的工具:
修改MySQL的配置文件,开启MySQL服务器的二进制(binlog)的日志功能
[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 110684
-rw-r-----. 1 mysql mysql 56 2月 20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql 1406 2月 21 14:49 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:49 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 14:49 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 54161 2月 21 14:49 localhost.localdomain.err
drwxr-x---. 2 mysql mysql 4096 2月 21 14:45 mysql
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 performance_schema
drwxr-x---. 2 mysql mysql 58 2月 21 14:45 school
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 sys
[root@localhost ~]# 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 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# 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_log //开启二进制日志功能
[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 123056
-rw-r-----. 1 mysql mysql 56 2月 20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql 561 2月 22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:08 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 22 14:08 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:08 ibtmp1
-rw-r-----. 1 mysql mysql 75021 2月 22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql 4096 2月 21 14:45 mysql
-rw-r-----. 1 mysql mysql 154 2月 22 14:08 mysql_log.000001
-rw-r-----. 1 mysql mysql 19 2月 22 14:08 mysql_log.index
-rw-r-----. 1 mysql mysql 5 2月 22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 performance_schema
drwxr-x---. 2 mysql mysql 58 2月 21 14:45 school
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 sys
对数据库进行全量备份
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-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 |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.05 sec)
mysql> show tables from school;
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
mysql> select * from school.student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
+----+-------+------+
5 rows in set (0.07 sec)
mysql> exit
Bye
[root@localhost ~]# mysqldump -uroot -pzml123! --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-scholl201902221415
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
all-scholl201902221415 anaconda-ks.cfg
在数据库中添加新内容
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-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> use school
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 student values (6,'dsz',9);
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 9 |
+----+-------+------+
6 rows in set (0.00 sec)
mysql> update student set age = 19 where id = 6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
+----+-------+------+
6 rows in set (0.00 sec)
mysql> exit
Bye
模拟误删数据库
[root@localhost ~]# mysql -e 'drop database school;'
[root@localhost ~]# mysql -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
刷新创建新的二进制日志
[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 123004
-rw-r-----. 1 mysql mysql 56 2月 20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql 561 2月 22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 22 14:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql 75021 2月 22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql 4096 2月 21 14:45 mysql
-rw-r-----. 1 mysql mysql 870 2月 22 14:25 mysql_log.000002
-rw-r-----. 1 mysql mysql 19 2月 22 14:15 mysql_log.index
-rw-r-----. 1 mysql mysql 5 2月 22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 sys
[root@localhost data]# mysqladmin flush-logs
[root@localhost data]# ll
总用量 123008
-rw-r-----. 1 mysql mysql 56 2月 20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql 561 2月 22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 22 14:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql 75021 2月 22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql 4096 2月 21 14:45 mysql
-rw-r-----. 1 mysql mysql 917 2月 22 14:26 mysql_log.000002
-rw-r-----. 1 mysql mysql 154 2月 22 14:26 mysql_log.000003
-rw-r-----. 1 mysql mysql 38 2月 22 14:26 mysql_log.index
-rw-r-----. 1 mysql mysql 5 2月 22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 sys
恢复完全备份
[root@localhost ~]# mysql < all-scholl201902221415
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
+----+-------+------+
恢复差异备份
[root@localhost ~]# ll /opt/data/
总用量 124028
-rw-r-----. 1 mysql mysql 56 2月 20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql 561 2月 22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:30 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 22 14:30 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql 75021 2月 22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql 4096 2月 22 14:30 mysql
-rw-r-----. 1 mysql mysql 917 2月 22 14:26 mysql_log.000002
-rw-r-----. 1 mysql mysql 784883 2月 22 14:30 mysql_log.000003
-rw-r-----. 1 mysql mysql 38 2月 22 14:26 mysql_log.index
-rw-r-----. 1 mysql mysql 5 2月 22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 performance_schema
drwxr-x---. 2 mysql mysql 58 2月 22 14:30 school
drwxr-x---. 2 mysql mysql 8192 2月 20 16:23 sys
//检查误删数据库的位置在什么地方
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.23-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 binlog events in 'mysql_log.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_log.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.23-log, Binlog ver: 4 |
| mysql_log.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql_log.000002 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_log.000002 | 219 | Query | 1 | 293 | BEGIN |
| mysql_log.000002 | 293 | Table_map | 1 | 349 | table_id: 141 (school.student) |
| mysql_log.000002 | 349 | Write_rows | 1 | 394 | table_id: 141 flags: STMT_END_F |
| mysql_log.000002 | 394 | Xid | 1 | 425 | COMMIT /* xid=472 */ |
| mysql_log.000002 | 425 | Anonymous_Gtid | 1 | 490 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_log.000002 | 490 | Query | 1 | 564 | BEGIN |
| mysql_log.000002 | 564 | Table_map | 1 | 620 | table_id: 141 (school.student) |
| mysql_log.000002 | 620 | Update_rows | 1 | 676 | table_id: 141 flags: STMT_END_F |
| mysql_log.000002 | 676 | Xid | 1 | 707 | COMMIT /* xid=474 */ |
| mysql_log.000002 | 707 | Anonymous_Gtid | 1 | 772 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_log.000002 | 772 | Query | 1 | 870 | drop database school |
| mysql_log.000002 | 870 | Rotate | 1 | 917 | mysql_log.000003;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)
mysql> exit
Bye
//使用mysqlbinlog恢复差异备份
[root@localhost ~]# mysqlbinlog --stop-position=772 /opt/data/mysql_log.000002 | mysql
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
+----+-------+------+
[root@localhost ~]#