2019-06-10数据库sql语句

数据库sql语句

进入mysql

[root@web02 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

查看数据库中的所有数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 

查看mysql数据库中的所有表

MariaDB [(none)]> show tables from mysql;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

查看mysql数据库中user表中的user和hosts字段。

MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user      | host       |
+-----------+------------+
| root      | 127.0.0.1  |
| root      | ::1        |
| root      | localhost  |
| root      | web02      |
+-----------+------------+
5 rows in set (0.04 sec)

查看当前在以哪个用户的身份登录的数据库

MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.03 sec)

进入mysql数据库

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

查看当前在使用的数据库

MariaDB [mysql]> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

创建一个wordpress数据库

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.03 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)

创建两个用户

授权wordpress数据库的所有权限(增删改查)

MariaDB [(none)]> grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456' ;
Query OK, 0 rows affected (0.25 sec)
MariaDB [(none)]> grant all on wordpress.* to wordpress@'locallhost' identified by '123456' ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user | host |
+-----------+------------+
| root | 127.0.0.1 |
| wordpress | 172.16.1.% |
| root | ::1 |
| root | localhost |
| wordpress | locallhost |
| root | web02 |
+-----------+------------+
6 rows in set (0.00 sec)

备份数据库

[root@web02 ~]# mysqldump -uroot -A >/root/all.sql
[root@web02 ~]# ll /root
total 22420
-rw-r--r-- 1 root root 515388 Jun 10 19:07 all.sql

删除wordpress 数据库的管理用户

MariaDB [(none)]> drop user wordpress@'172.16.1.%' ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user wordpress@'locallhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
| root | web02 |
+------+-----------+
4 rows in set (0.00 sec)

删除wordpress数据库

MariaDB [(none)]> drop databases wordpress;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'databases wordpress' at line 1
MariaDB [(none)]> drop database wordpress;
Query OK, 0 rows affected (0.00 sec)

使用数据库备份文件进行恢复数据库 并刷新用户权限

[root@web02 ~]# mysql 

进入数据库进行检查

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user | host |
+-----------+------------+
| root | 127.0.0.1 |
| wordpress | 172.16.1.% |
| root | ::1 |
| root | localhost |
| wordpress | locallhost |
| root | web02 |
+-----------+------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

如果修改用户后没有刷新用户权限,会导致用户无法使用

[root@web02 ~]# mysql -uwordpress -p
Enter password:
ERROR 1045 (28000): Access denied for user 'wordpress'@'localhost' (using password: YES)

备份数据库直接为压缩包

[root@web01 ~]# mysqldump -uroot -p -A |gzip >/root/all-gzip.sql.gz
Enter password:
[root@web01 ~]# ll /root/all-gzip.sql.gz
-rw-r--r-- 1 root root 139656 Jun 10 11:21 /root/all-gzip.sql.gz
[root@web01 ~]# ll -h /root/all-gzip.sql.gz
-rw-r--r-- 1 root root 137K Jun 10 11:21 /root/all-gzip.sql.gz

你可能感兴趣的:(2019-06-10数据库sql语句)