MySQL用户及权限管理

来源:互联网 发布:淘宝申请退款流程 编辑:程序博客网 时间:2024/04/28 19:44

一、 MySQL初始密码

新安装的MySQL默认是没有密码的,设置初始密码可以用以下命令

mysqladmin -u root password 'new-password'   mysqladmin -u root password 'password' -S /data/3306/mysql.sock #socket多实例
设置初始密码后,可在当前用户家目录下创建.my.cnf文件,配置好用户名和密码后,该用户可以免密码登录,要注意设置文件权限,不要让其他用户看到密码信息

vi ~/.my.cnf[client]   user=root   host=localhost   password=cathy 

MySQL服务器在读取配置文件的时候是按照以下顺序读取的:

/etc/my.cnf --> /etc/mysql/my.cnf --> $MYSQL_HOME/my.cnf --> --default-extra-file=/path/to/somefile--> ~/.my.cnf

如果不同配置文件有相同配置选项则以最后一个配置文件为准

二、修改root密码

方法一:命令行

mysqladmin -u root -p oldpassword password 'password' #单实例mysqladmin -u root -p oldpassword password 'password'-S /data/3306/mysql.sock#多实例

方法二:登录mysql

mysql> update mysql.user set Password=password("storm") where User="root"mysql> flush privileges
此方法有风险,必须指定条件和password函数, 否则有可能会其他用户的密码一并改掉
方法三:
myslq> set password=password(456) # 修改当前登录用户密码set password for 'storm'@'%' =password(456) # 修改指定用户密码myslq> flush privileges

三、重置忘记的密码

1. 停止mysqld服务
/etc/init.d/mysqld stop
2. 启动mysqld,忽略授权表
mysqld_safe --skip-grant-tables --skip-networking --user=mysql  &

--skip-grant-tables表示忽略授权表启动MySQL

--skip-networking表示不连入网络,防止无密码启动mysql带来安全隐患

3. 登录mysql修改密码

mysqlmysql> update mysql.user set Password=password("newpassword") where User="root"mysql> flush privileges

注意:二进制包安装MySQL,在执行mysqld_safe --skip-grant-tables命令时会出现如下错误:

[root@dev ~]# mysqld_safe160419 20:32:08 mysqld_safe Logging to '/usr/local/mysql/data/dev.err'.touch: cannot touch `/usr/local/mysql/data/dev.err': No such file or directorychmod: cannot access `/usr/local/mysql/data/dev.err': No such file or directorytouch: cannot touch `/usr/local/mysql/data/dev.err': No such file or directorychown: cannot access `/usr/local/mysql/data/dev.err': No such file or directory160419 20:32:08 mysqld_safe The file /usr/local/mysql/bin/mysqlddoes not exist or is not executable. Please cd to the mysql installationdirectory and restart this script from there as follows:./bin/mysqld_safe&See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information/application/mysql/bin/mysqld_safe: line 128: /usr/local/mysql/data/dev.err: No such file or directory
错误原因暂时未找到,但是仔细看报错信息:Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe&
意思是要进入MySQL的安装目录再执行mysqld_safe命令,我尝试进入我安装的MySQL目录:/application/mysql/bin/然后执行mysqld_safe依然报错,最后发现只有在MySQL的安装目录下执行./bin/mydqld_safe才能正常执行,具体原因还有待查找!

4. 重启mysql

/etc/init.d/mysqld stop/etc/init.d/mysqld start

若修改完密码后无法关闭,用mysqladmin -uroot -pnewpassword shutdown关闭

多实例MySQL启动修改丢失的root密码

mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables&mysql -uroot -p -S /data/3306/mysql.sock  # 登录时用空密码
四、创建用户及授权
方法一:先创建用户再授权

mysql> create user 'storm'@'localhost' IDENTIFIED BY 'mypass';mysql> grant all on db1.* to 'jeffrey'@'localhost';
方法二:直接授权给用户,用户不存在时则创建
mysql> grant all on dbname.* to 'jeffrey'@'localhost' identified by 'password'
1. 用户名格式

        MySQL创建用户时,用户名的格式为:'用户名'@'主机' ,其中用户名和主机名的引号可用单引号,也可用双引号,但是不可省略。主机权限范围例举如下:

‘storm'@'%'

#表示所有主机均可使用storm账户登录

mysql ‘storm'@'192.168.0.% 

 #表示只允许192.168.0网段的用户连接,这种情况即使本地连接mysql,也需要指定-h IP的方式连接

mysql‘storm'@'localhost’

#表示只允许本地连接

2. MySQL用户权限

        MySQL可授权的权限有:SELECT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS,FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES,LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOWVIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, all表示所有权限。

为用户授权时,可精确到数据库、数据库所对应的表、表里的某个字段的具体权限,如:

grant all on *.* to ‘storm'@'%' 

#为storm授予所有库的所有操作权限

grant all on school.* to ‘storm'@'%' 

#为storm授予school数据库下的所有表的所有操作权限

grant select, insert,update on school.class to ‘storm'@'%' 

#为storm授予school数据库下class表的 select, insert,update权限

grant update(age) on on school.class to ‘storm'@'%'  

#表示storm用户只能update的字段为age

3. 查看用户权限

mysql> show grants for storm@'%';
4. 撤回用户权限

REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
5. 删除用户

mysql> drop user 'root'@'127.0.0.1';


0 0