查看关于权限的命令
mysql> help show grants
mysql> SHOW GRANTS FOR ‘root‘@‘localhost‘;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON . TO ‘root‘@‘localhost‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION |
+---------------------------------------------------------------------+
设置及修改mysql密码
新建mysql需要按照安全策略
1 设置mysql root密码
2 删除mysql里无用的账号
3 删除默认的test数据库
增加新账号为管理员即和root等价的用户
mysql> grant all privileges on . to system@‘localhost‘ identified by ‘P@ssw0rd‘ with grant option;
删除所有用户
mysql> delete from mysql.user;
为管理员root用户设置密码
mysqladmin -u root password‘P@ssw0rd‘ 没有密码用户设置密码
mysqladmin -uroot -pP@ssw0rd password ‘swat112@‘ -S /data/mysql/3306/sockt/mysql.sock3306 适用于多实例
修改管理密码
1 指定用户
mysqladmin -u root -pP@ssw0rd password ‘swat112@‘
mysqladmin -uroot -pP@ssw0rd password ‘swat112@‘ -S /data/mysql/3306/sockt/mysql.sock3306
2 必须指定where 必须指定password()函数来更改密码
update mysql.user set password=‘P@ssw0rd‘ where user=‘root‘ and host=‘localhost‘;
flush privileges;
update mysql.user set authentication_string=‘P@ssw0rd‘ where user=‘root‘ and host=‘localhost‘;
flush privileges;
如果需要加密
update mysql.user set password=pasword(P@ssw0rd) where user=‘root‘ and host=‘localhost‘;
update mysql.user set authentication_string=password(P@ssw0rd) where user=‘root‘ and host=‘localhost‘;
一般情况mysql 安装完成后 root为空密码 休要马上修改密码
set password=password(‘swat112@‘);
flush privileges;
此法不适合-skip-grant-tables方式修改
通过grant命令创建用户并授权
1 grant 命令简单语法如下
br/>说明:
localhost访问数据库localhost是本机的意思,如果让用户任何地方访问可以用%
注意用户用‘’框住‘stonehu‘@‘%‘
dbname是数据库名称,譬如只对test库的table1有访问权限可以表示test.table1,对所以库和表有访问权限,.
username是用户名的意思,其中username dbname passwd可根据业务的情况修改
实例1
mysql> grant all privileges on . to ‘stonehu‘@‘%‘ identified by ‘P@ssw0rd‘;
mysql> flush privileges;
Create和grant配合法
1首先创建用户username passwd 授权主机localhost
mysql> create user ‘test‘@‘localhost‘ identified by ‘P@ssw0rd‘;
mysql> create database test1;
mysql> grant all on test1. to ‘test‘@‘localhost‘;
查看某个用户权限
想要看某个人的权限,有不知道名字可以参考以下步骤
mysql> select user,host from mysql.user;
mysql> show grants for ‘stonehu‘@‘%‘;
授权某个用户某个网络访问主机
如果192.168.48.100访问本机localhost换成192.168.48.100,任何地方可以以%表示
某个网段可以以10.0.0.%表示或者10.0.0.0/24
192.168.48.0/255.255.255.0
mysql> grant all on test1. to ‘test‘@‘192.168.48.100‘ identified by ‘P@ssw0rd‘;
all privileges权限
all privileges权限有哪些
想要了解mysql all privileges有哪些可以用 show privileges查询
撤销权限revoke
mysql> revoke insert on test1. from ‘test‘@‘localhost‘;
mysql> show grants for ‘test‘@‘localhost‘;
不进数据库显示用户权限
[root@mysqlmu ~]# mysql -uroot -pP@ssw0rd -e "show grants for test@localhost;"
[root@mysqlmu ~]# mysql -uroot -pP@ssw0rd -e "show grants for test@localhost;" |grep -i grant
[root@mysqlmu ~]# mysql -uroot -pP@ssw0rd -e "show grants for test@localhost;" |grep -i grant |tail -1
将,换成回车 tr ‘,’ ‘\n’ 导出文本
[root@mysqlmu ~]# mysql -uroot -pP@ssw0rd -e "show grants for test@localhost;" |grep -i grant |tail -1|tr ‘,‘ ‘\n‘ > all.txt
[root@mysqlmu ~]# cat all.txt -n
查看用户权限
注意host db user
mysql> use mysql
mysql> show tables;
mysql> desc user;
看详细可以用desc user\G
或者可以用
mysql> select from mysql.user\G
MySQL管理-授权(14)
原文:https://blog.51cto.com/huxiaoqiu/2540240