[root@zlinux ~]# 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
:設定密碼,用單引號括起來。mysql
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@zlinux ~]# 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@zlinux ~]# 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>
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 user02@127.0.0.1 | +------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'user02'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | +------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
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)
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> 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@zlinux ~]# mysqldump -uroot -pzlinux123456 mysql >/tmp/mysql.sql //備份 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# ls /tmp/mysql.sql /tmp/mysql.sql [root@zlinux ~]# mysql -uroot -pzlinux123456 db2 < /tmp/mysql.sql //恢復 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# mysql -uroot -pzlinux123456 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@zlinux ~]# mysqldump -uroot -pzlinux123456 mysql user > /tmp/mysqluser.sql //備份mysql庫user表 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# mysql -uroot -pzlinux123456 db1 < /tmp/mysqluser.sql //恢復到db1 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# mysql -uroot -pzlinux123456 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>