使用mysql 1、登錄,可以用密碼登錄,也可以不用密碼登錄。命令格式“mysql –u 用戶名 –p 密碼” [root@localhost src]# mysql -u root –p //有密碼登錄 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、創(chuàng)建數(shù)據(jù)庫,命令“create database 數(shù)據(jù)庫名稱;”,注意這個命令后面有分號 mysql> create database test1; Query OK, 1 row affected (0.00 sec) |
4、查看數(shù)據(jù)庫,命令“show databases;” mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test1 | +--------------------+ 4 rows in set (0.00 sec) |
5、刪除數(shù)據(jù)庫,命令“drop database 數(shù)據(jù)庫名稱;” mysql> drop database test1; Query OK, 0 rows affected (0.01 sec) |
6、設置權限 mysql允許給某個特定的用戶賦予指定的權利,而且可以指定在某臺機器上使用。Mysql的權限如下 權限 | 數(shù)據(jù)庫 | Table | Column | 說明 | all privileges | √ |
|
| 所有權利 | alter | √ | √ |
| 增減、刪除、修改列 | create | √ | √ |
| 創(chuàng)建數(shù)據(jù)庫、表 | delete | √ | √ |
| 刪除行 | drop | √ | √ |
| 刪除表、數(shù)據(jù)庫 | file | √ |
|
| 操作文件 | index | √ | √ |
| 索引 | insert | √ | √ | √ | 插入 | process | √ |
|
| 查看線程、連接 | reference | √ |
|
| 創(chuàng)建外鍵 | reload | √ |
|
| 重新加載,擁有此權限可以刷新表 | select | √ | √ | √ | 選擇 | shutdown | √ |
|
| 關閉 | update | √ | √ | √ | 更新 | usage | √ |
|
| 無權限,只能連接 |
1)授權用戶權限,命令格式“grant 權限on 數(shù)據(jù)庫文件to 用戶名@ip identified by ‘密碼’;”。在使用grant的時候,如果用戶不存在,那么久創(chuàng)建用戶。 //給david在本機授權插入功能,密碼123456,只能對test01操作 mysql> grant insert on test01.* to david@localhost identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> //給david所有權限,在所有的主機都可以操作,而且可以操作任意數(shù)據(jù)庫 mysql> grant all privileges on *.* to david@'%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> |
2)查看當前數(shù)據(jù)庫所有授權情況,命令“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 數(shù)據(jù)庫文件 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; //首先要調(diào)用這個命令 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> |
[color=rgb(68, 68, 6  ] ![]()
|