Linux伺服器---使用mysql

一生有你llx發表於2018-12-01

使用mysql

1 、登入,可以用密碼登入,也可以不用密碼登入。命令格式“mysql –u 使用者名稱 –p 密碼”

[root@localhost src]#  mysql -u root      // 有密碼登入

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

[root@localhost src]#  mysql -u root          // 無密碼登入

 

2 、退出,命令“quit” 

[root@localhost bin]#  quit

 

3 、建立資料庫,命令“create database 資料庫名稱;”,注意這個命令後面有分號

mysql>  create database test1;

Query OK, 1 row affected (0.00 sec)

 

4 、檢視資料庫,命令“show databases;”

mysql>  show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

| test1              |

+--------------------+

4 rows in set (0.00 sec)

 

5 、刪除資料庫,命令“drop database 資料庫名稱;”

mysql>  drop database test1;

Query OK, 0 rows affected (0.01 sec)

 

6 、設定許可權 

mysql 允許給某個特定的使用者賦予指定的權利,而且可以指定在某臺機器上使用。Mysql的許可權如下

許可權

資料庫

Table

Column

說明

all privileges



所有權利

alter


增減、刪除、修改列

create


建立資料庫、表

delete


刪除行

drop


刪除表、資料庫

file



操作檔案

index


索引

insert

插入

process



檢視執行緒、連線

reference



建立外來鍵

reload



重新載入,擁有此許可權可以重新整理表

select

選擇

shutdown



關閉

update

更新

usage



無許可權,只能連線

 

1 )授權使用者許可權,命令格式“grant 許可權on 資料庫檔案to 使用者名稱@ip identified by ‘密碼’;”。在使用grant的時候,如果使用者不存在,那麼久建立使用者。

// david 在本機授權插入功能,密碼 123456 ,只能對 test01 操作

mysql>  grant insert on test01.* to david@localhost identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> 

// david 所有許可權,在所有的主機都可以操作,而且可以操作任意資料庫

mysql>  grant all privileges on *.* to david@'%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql>

2 )檢視當前資料庫所有授權情況,命令“select host,user from mysql.user” 

mysql>  select host,user from mysql.user;

+-----------------------+-------+

| host                  | user  |

+-----------------------+-------+

| %                     | david |

| 127.0.0.1             | root  |

| localhost             |       |

| localhost             | david |

| localhost             | root  |

| localhost.localdomain |       |

| localhost.localdomain | root  |

+-----------------------+-------+

7 rows in set (0.00 sec)

mysql> 

3 )檢視當前登入使用者的權利,命令“show grants” 

mysql>  show grants;

+----------------------------------------------------------------------------------------------------------------------------------------+

| Grants for root@localhost                                                                                                             |

+----------------------------------------------------------------------------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*1256939B1977AFF6C3D114C5594EE354EF363A8B' WITH GRANT OPTION |

+----------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> 

4 )檢視某個使用者在某臺機器的許可權,命令“show grants for user@ip” 

mysql>  show grants for david@localhost;

+--------------------------------------------------------------------------------------------------------------+

| Grants for david@localhost                                                                                  |

+--------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'david'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |

| GRANT INSERT ON `test01`.* TO 'david'@'localhost'                                                            |

+--------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

mysql> 

5 )刪除使用者的許可權,命令“revoke 許可權on  資料庫檔案  from  user@ip”

mysql>  revoke all privileges on *.* from david@'%'; 

Query OK, 0 rows affected (0.00 sec)

 

mysql>  show grants for david@localhost;        // 刪除之後檢視一下

+--------------------------------------------------------------------------------------------------------------+

| Grants for david@localhost                                                                                  |

+--------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'david'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |

+--------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

mysql>         

6 )刪除使用者,命令“delete from user where user=‘username’”

mysql>  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

Database changed

 

mysql>  delete from user where user='david';    // 刪除使用者

Query OK, 2 rows affected (0.00 sec)

 

mysql>  select host,user from mysql.user;     // 檢視使用者

+-----------------------+------+

| host                  | user |

+-----------------------+------+

| 127.0.0.1             | root |

| localhost             |      |

| localhost             | root |

| localhost.localdomain |      |

| localhost.localdomain | root |

+-----------------------+------+

5 rows in set (0.00 sec)

mysql> 


做了一個Linux學習的平臺,目前出來一個雛形,各位可以參考使用
連結: https://pan.baidu.com/s/1GOLVU2CbpBNGtunztVpaCQ   密碼:n7bk



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29270124/viewspace-2222357/,如需轉載,請註明出處,否則將追究法律責任。

相關文章