mysql进阶

mysql进阶

  • 1.mysql配置文件
  • 2. mysql数据库备份
    • 2.1 数据库常用备份方案
    • 2.2 mysql备份工具mysqldump
    • 2.3 mysql数据恢复
    • 2.4 mysql能用localhost连接,但无法使用127.0.0.1连接
    • 2.5 mysql授权连接和撤销

1.mysql配置文件

  • mysql的配置文件为/etc/my.cnf

配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
//不需要输入密码和用户名登陆

在本地创建一个.my.cnf文件,将用户名和密码填入
[root@xaii ~]# vim .my.cnf 
[root@xaii ~]# cat .my.cnf
[client]
user=root
password=lizhao123!
[root@xaii ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
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> 

mysql常用配置文件参数:

参数 说明
port = 3306 设置监听端口
socket = /tmp/mysql.sock 指定套接字文件位置
datadir = /data/mysql 指定MySQL的数据存放路径
basedir = /usr/local/mysql 指定MySQL的安装路径
user = mysql 指定MySQL以什么用户的身份提供服务
skip-name-resolve 禁止MySQL对外部连接进行DNS解析

2. mysql数据库备份

2.1 数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案 特点
全量备份 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。数据恢复快。备份时间长
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件,如此类推。没有重复的备份数据;备份时间短;恢复数据时必须按一定的顺序进行
差异备份 备份上一次的完全备份后发生变化的所有文件。差异备份是指在一次全备份后到进行差异备份的这段时间内对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。

2.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#					//指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3308

//备份整个数据库(全量备份)

[root@xaii ~]# service mysqld start
Starting MySQL....... SUCCESS! 
[root@xaii ~]# mysql -uroot -plizhao123!
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.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> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lizhao             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use lizhao;
Database changed
mysql> show tables;
+------------------+
| Tables_in_lizhao |
+------------------+
| DrinkWater       |
| soccer           |
| student          |
+------------------+
3 rows in set (0.00 sec)

[root@xaii ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201902211500.sql
Enter password: 

[root@xaii ~]# ls
all-201902211500.sql  anaconda-ks.cfg

//备份lizhao库的soccer表和student表

[root@xaii ~]# mysqldump -uroot -p -h127.0.0.1 lizhao soccer student > table-201902211500.sql
Enter password: 

[root@xaii ~]# ls
all-201902211500.sql  anaconda-ks.cfg  table-201902211500.sql

//备份lizhao库

[root@xaii ~]# mysqldump -uroot -p -h127.0.0.1 --databases lizhao > lz-201902211500.sql
Enter password: 

[root@xaii ~]# ls
all-201902211500.sql  anaconda-ks.cfg  lz-201902211500.sql  table-201902211500.sql

//模拟误删lizhao数据库

mysql> drop database lizhao;
Query OK, 3 rows affected (0.32 sec)

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

2.3 mysql数据恢复

//恢复lizhao数据库

[root@xaii ~]# ls
all-201902211500.sql  anaconda-ks.cfg  lz-201902211500.sql  table-201902211500.sql

[root@xaii ~]# mysql -uroot -p -h127.0.0.1 < all-201902211500.sql 
Enter password: 

[root@xaii ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lizhao             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

//恢复lizhao数据库的soccer表和student表

mysql> use lizhao;
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> 
mysql> source table-201902211500.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)

mysql> show tables;
+------------------+
| Tables_in_lizhao |
+------------------+
| DrinkWater       |
| soccer           |
| student          |
+------------------+
3 rows in set (0.00 sec)

2.4 mysql能用localhost连接,但无法使用127.0.0.1连接

解决方案:

关闭SELINUX

  • setenforce 0

  • 编辑配置文件

     vim /etc/selinux/config	
     SELINUX=disabled
    

2.5 mysql授权连接和撤销

授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 
mysql> grant all privileges on *.* to 'root'@'%' identified by 'lizhao123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//说明:此命令是为密码为 root 、IP(%)任意的 root 用户授权。(%:模糊查询,所有 IP 都可以,,可指定其他主机 IP;BY 后的 'root' 为密码)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:
mysql>revoke all on *.* from dba@localhost;
mysql> revoke all privileges from *.* to 'root'@'%' identified by 'lizhao123!';

你可能感兴趣的:(数据库,mysql,数据库备份,linux)