MySQL数据库命令行用户管理
登录MySQL数据库
- 登录MySQL
[root@dstest2-db ~]# mysql -uroot -p密码
字段 | 描述 |
---|---|
root | 最高权限用户名 |
密码 | 登录密码 |
2. 选择mysql数据库
mysql> use mysql;
... ...
Database changed
- 查看数据库所有表
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
... ...
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql>
- 查看当前用户
mysql> select host , user from user ;
+---------------+---------------+
| host | user |
+---------------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+---------------+---------------+
3 rows in set (0.00 sec)
mysql>
创建用户
命令:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
字段 | 描述 | 词典 |
---|---|---|
username | ☞将创建的用户名 | |
host | ☞指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,从任意远程主机登陆可以使用通配符% | localhost % |
password | 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。 |
授权用户
- 将数据库实例授权给用户
# 命令:
GRANT privileges ON databasename.tablename TO 'username'@'host';
字段 | 描述 | 词典 |
---|---|---|
privileges | 用户的操作权限,授予所的权限则使用ALL。 | SELECT INSERT UPDATE 等(参见权限表) ALL |
databasename | 数据库名 | |
tablename | 表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*.*表示 |
2. 修改用户连接权限
命令:
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "password" ;
创建一个拥有所有权限的root用户,可以远程任意主机连接。
权限操作表
权限 | 描述 |
---|---|
ALTER | Allows use of ALTER TABLE. |
ALTER ROUTINE | Alters or drops stored routines. |
CREATE | Allows use of CREATE TABLE. |
CREATE ROUTINE | Creates stored routines. |
CREATE TEMPORARY TABLE | Allows use of CREATE TEMPORARY TABLE. |
CREATE USER | Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES. |
CREATE VIEW | Allows use of CREATE VIEW. |
DELETE | Allows use of DELETE. |
DROP | Allows use of DROP TABLE. |
EXECUTE | Allows the user to run stored routines. |
FILE | Allows use of SELECT… INTO OUTFILE and LOAD DATA INFILE. |
INDEX | Allows use of CREATE INDEX and DROP INDEX. |
INSERT | Allows use of INSERT. |
LOCK TABLES | Allows use of LOCK TABLES on tables for which the user also has SELECT privileges. |
PROCESS | Allows use of SHOW FULL PROCESSLIST. |
RELOAD | Allows use of FLUSH. |
REPLICATION | Allows the user to ask where slave or master |
CLIENT | servers are. |
REPLICATION SLAVE | Needed for replication slaves. |
SELECT | Allows use of SELECT. |
SHOW DATABASES | Allows use of SHOW DATABASES. |
SHOW VIEW | Allows use of SHOW CREATE VIEW. |
SHUTDOWN | Allows use of mysqladmin shutdown. |
SUPER | Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached. |
UPDATE | Allows use of UPDATE. |
USAGE | Allows connection without any specific privileges. |