mysql创建用户并授权:
格式:grant 权限 on 数据库名.表名 to 用户@登录主机 identified by "用户密码";
grant[英][grɑ:nt] 承认; 同意; 准许; 授予;
例1:允许mk用户从localhost登录
1
2
|
mysql> grant all on book.* to mk1@localhost identified by "123456" ; #允许访问book数据库下的所有表, 只能访问book表,而且是同一个服务器的用户 |
允许mk2用户从任意远端主机连接mysql服务器:
1
2
3
|
mysql> grant all privileges on *.* to mk2@ '%' identified by '123456' with grant option ; # with grant option 意思是mk2用户可以把自己的权限下放给新创建的用户。 另外,加不加 privileges 都可以。%指的任意远程主机,不包括本地地址和localhost Flush privileges ; 刷新数据库 |
测试:
1
2
|
[root@xuegod64 ~]# mysql -u mk2 -h 192.168.1.63 -p123456 mysql> #登录正常 |
但是:
1
|
[root@xuegod63 ~]# mysql -u mk2 -h 192.168.1.63 -p123456 #不能登录 |
解决方法:
1
2
|
mysql> grant all privileges on *.* to 'mk2' @ '192.168.1.63' identified by '123456' with grant option ; [root@xuegod63 ~]# mysql -u mk2 -p123456 #不能登录 |
解决方法:
1
|
mysql> grant all privileges on *.* to 'mk2' @ 'localhost' identified by '123456' with grant option ; |
总结: % 指的是任意远程主机,不包括本地地址和localhost。 另外grant是立即生效。不需要执行:mysql> flush privileges; #手动更新命令
只有手动修改了mysql相关字段,才需要执行mysql> flush privileges;
只授权部分权限:
1
|
mysql> grant select , insert , update , delete , create , drop on aa.* to 'custom' @ 'localhost' identified by '123456' ; |
方法二: 直接修改表中权限文件:
1
2
3
4
5
6
7
8
|
mysql> use mysql; mysql> insert into user (Host, User , Password ) values ( 'localhost' , 'grace' , '123456' ); mysql> select Host, User , Password from user where User = "grace" ; + -----------+-------+----------+ | Host | User | Password | + -----------+-------+----------+ | localhost | grace | 123456 | + -----------+-------+----------+ |
可以看到密码是明文存放的,现在以加密方式存储:
1
2
3
4
5
6
7
8
9
10
|
mysql> insert into user (Host, User , Password ) values ( 'localhost' , 'grace1' , password ( "123456" )); Query OK, 1 row affected, 3 warnings (0.00 sec) mysql> select Host, User , Password from user where User = "grace1" ; + -----------+--------+-------------------------------------------+ | Host | User | Password | + -----------+--------+-------------------------------------------+ | localhost | grace1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | + -----------+--------+-------------------------------------------+ 1 row in set (0.01 sec) mysql> flush privileges ; #刷新权限表,使配置文件生效 |
或重启mysql 数据库
1
|
[root@xuegod63 ~]# service mysqld restart |
测试:
1
2
3
|
[root@xuegod63 ~]# mysql -u grace -p123456 #登录不成功 ERROR 1045 (28000): Access denied for user 'grace' @ 'localhost' (using password : YES) [root@xuegod63 ~]# mysql -u grace1 -p123456 #登录成功 |
修改帐户密码:
方法一:使用mysqladmin修改密码
例1:当root没有密码时:
1
2
|
[root@xuegod63 mysql]# mysqladmin -u root -h 192.168.1.63 password '123' [root@xuegod63 mysql]# mysql -u root -h 192.168.1.63 -p123 |
例2: 当root已经有密码时:
1
|
[root@xuegod63 ~]# mysqladmin -u root password '123456' -p123 |
方法二:使用 set password 修改密码:
1
2
3
4
|
mysql> SET PASSWORD FOR 'grace1' @ 'localhost' = PASSWORD ( '123456' ); #注,你的mysql库中已经有记录:grace1 '@' localhost mysql> set password = password ( '1234567' ); mysql> FLUSH PRIVILEGES ; |
重置root密码:
1
2
|
[root@xuegod63 mysql]# /etc/init.d/mysqld stop [root@xuegod63 mysql]# mysqld_safe --skip-grant-tables --skip-networking |
只在mysql5.1版本有效
重新再开一个终端: 直接就可以进去了, 然后用update修改密码
1
2
3
4
|
[root@xuegod63 aa]# mysql #执行 mysql> update mysql. user set password = password ( '123456' ) where host= 'localhost' and user = 'root' ; [root@xuegod63 aa]# /etc/init.d/mysqld restart Stopping mysqld: [ OK ] |
以上所述是小编给大家介绍的mysql 5.1版本修改密码及远程登录mysql数据库的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!