MySQL数据库操作(1)用户与权限

MySQL安装:
sudo apt-get install mysql-server mysql-client

 

本地通过命令行进入MySQL:

mysql -u用户名 -p密码
mysql -u用户名 -p 回车后输入密码

退出MySQL:
\q
exit
强行退出,Ctrl+Z

创建用户:
CREATE USER '用户名'@'域名' IDENTIFIED BY '密码';
域名:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%
密码:密码可以为空,如果为空则该用户可以不需要密码登陆服务器。
例子:
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';

删除用户:
DROP USER '用户名'@'域名';
例子:
DROP USER 'pig'@'192.168.1.101';
DROP USER 'pig'@'%';

给用户授权:
GRANT 权限 ON 数据库名.表名 TO '用户名'@'域名';
让该用户可以对指定数据库下指定的表,进行指定的操作,每项权限之间用逗号分隔,权限表见结尾
例子:
GRANT SELECT, INSERT ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';
注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT 权限 ON 数据库名.表名 TO '用户名'@'域名' WITH GRANT OPTION;

撤销用户权限:
REVOKE 权限 ON 数据库名.表名 FROM '用户名'@'域名';
例子:
REVOKE SELECT ON *.* FROM 'pig'@'%';
注意:
撤销的权限和库表必须是和授权时一致,如果撤销的权限和授权时不一致,则不会撤销该权限。

显示用户权限:
SHOW GRANTS FOR '用户名'@'域名';
例子:
SHOW GRANTS FOR 'pig'@'%';

设置与更改用户密码:
对指定用户 SET PASSWORD FOR '用户名'@'域名' = PASSWORD('新密码');
对当前用户 SET PASSWORD = PASSWORD("新密码");
例子:
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");

查看有哪些数据库:
SHOW DATABASES;

查看当前在哪个数据库里:
SELECT DATABASE();

查看当前用户:
SELECT USER();

查看用户列表:
select host,user from mysql.user;

 

附表:在MySQL中的操作权限 

 

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 USERDROP USERRENAME 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 MASTERKILLPURGE 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.

posted @ 2018-01-17 23:06  时代海豚  阅读(244)  评论(0编辑  收藏  举报