1. 程式人生 > >96.創建普通用戶並授權,常用SQL語句,MySQL數據庫備份與恢復

96.創建普通用戶並授權,常用SQL語句,MySQL數據庫備份與恢復

創建普通用戶並授權常用SQL語句 MySQL數據庫備份與恢復

一、創建普通用戶並授權

1、創建用戶並授權

[root@sdwaqw ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> grant all on . to user01 identified by ‘123456‘
-> ; //上一行命令忘記輸入‘;’,這裏可以輸入後繼續執行
Query OK, 0 rows affected (0.01 sec)

創建user用戶並授予其所有權限.(第一個表示所有數據庫,第二個表示所有表)
這裏的user01特指localhost上的user01
identified by :設定密碼,用單引號括起來。

2、給網絡上其他機器某個用戶授權

mysql> grant all on . to ‘user02‘@‘127.0.0.1‘ identified by ‘123456‘; //指定IP,即只可通過此IP登錄,用戶和主機之間有個@。可以使用通配符%,代表所有IP(一般不使用)
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@sdwaqw ~]# mysql -uuser02 -p123456 //未連接到指定IP,登錄報錯

Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘user02‘@‘localhost‘ (using password: YES)
[root@sdwaqw ~]# mysql -uuser02 -p123456 -h127.0.0.1 //指定IP,無誤
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 9
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

3、查看用戶授權

mysql> show grants          //查看當前用戶授權
    -> ;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*A89494294B2411291D21451D05BAB332A65AAB5D‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show grants for ‘user02‘@‘127.0.0.1‘;      //查看指定用戶授權
+------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                            |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘user02‘@‘127.0.0.1‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4、授權部分權限(如讀、寫、查詢、插入等)
mysql> grant SELECT, INSERT, UPDATE *.* user03 identified by ‘123456‘;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘*.* user03 identified by ‘123456‘‘ at line 1
mysql> grant SELECT, INSERT, UPDATE on *.* to user03 identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for user03
    -> ;
+------------------------------------------------------------------------------------------------------------------------+
| Grants for user03@%                                                                                                    |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE ON *.* TO ‘user03‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
+------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

二、常用SQL語句

select count(*) from mysql.user; //查詢mysql庫中user表的行數select * from mysql.db; //查詢mysql庫中db表的所有內容select db from mysql.db; //查詢mysql庫中db表的db字段select db,user from mysql.db; 查詢mysql庫中db表的db和user字段select * from mysql.db where host like ‘192.168.%‘; //查詢mysql庫中db表中匹配192.128.開頭的內容insert into db1.t1 values (1, ‘abc‘); //在db1數據庫的t1表中插入對應 內容update db1.t1 set name=‘aaa‘ where id=1; //更改db1數據庫的t1表中name列內容,當id是1的時候truncate table db1.t1; //清除表內數據drop table db1.t1; //刪除表drop database db1; //刪除數據庫

三、MySQL數據庫備份與恢復

mysql> create database db1;    //創建庫
Query OK, 1 row affected (0.01 sec)

mysql> use db1;     //切換庫
Database changed  
mysql> create table t1 (`id` int(4),`name` char(40));                   //創建表及字段
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+1 row in set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

mysql> quit
Bye
[root@sdwaqw ~]# mysqldump -uroot -psdwaqw123456 mysql >/tmp/mysql.sql     //備份
Warning: Using a password on the command line interface can be insecure.
[root@sdwaqw ~]# ls /tmp/mysql.sql
/tmp/mysql.sql
[root@sdwaqw ~]# mysql -uroot -psdwaqw123456 db2 < /tmp/mysql.sql        //恢復
Warning: Using a password on the command line interface can be insecure.
[root@sdwaqw ~]# mysql -uroot -psdwaqw123456
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 29
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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 db2                  //切換到db2,查看是否恢復成功
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_db2             |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| t1                        |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+29 rows in set (0.01 sec)

mysql> quit
Bye
[root@sdwaqw ~]# mysqldump -uroot -psdwaqw123456 mysql user > /tmp/mysqluser.sql     //備份mysql庫user表
Warning: Using a password on the command line interface can be insecure.
[root@sdwaqw ~]# mysql -uroot -psdwaqw123456 db1 < /tmp/mysqluser.sql      //恢復到db1
Warning: Using a password on the command line interface can be insecure.
[root@sdwaqw ~]# mysql -uroot -psdwaqw123456
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 32
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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 db1    //查看是否成功恢復
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_db1 |
+---------------+
| t1            |
| user          |
+---------------+2 rows in set (0.00 sec)

mysql>

96.創建普通用戶並授權,常用SQL語句,MySQL數據庫備份與恢復