加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

mysql 设置root密码的方法总结

(2015-10-18 15:06:04)
标签:

mysql

mysql修改密码

mysql修改密码方法汇

分类: database
1)  用mysqladmin 设置或修改密码

[root@mysql ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.46 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \q
Bye

从上边可以看到登陆mysql不需要密码,
接下来是mysql 首次安装后 设置密码
[root@mysql ~]# /opt/soft/mysql/bin/mysqladmin -u root password 'password';
设置成功后不会有任何提示信息. 再尝试登陆发现无法登陆了.

[root@mysql ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

只能用这种方法登陆了 
[root@mysql ~]# mysql -u root -p
Enter password: 

设置完密码后如果想要修改密码怎么办呢? 如下  比如我将密码改为123  ,那么在出现提示输入密码时,输入上边设置的密码就可以了.
[root@mysql ~]# /opt/soft/mysql/bin/mysqladmin -u root password '123' -p
Enter password:

2)  登陆mysql后 用mysql命令  设置密码

接上例  我在将密码改为 789  
mysql> set PASSWORD FOR 'root'@'localhost' =PASSWORD('789');
Query OK, 0 rows affected (0.00 sec)


3) 通过修改数据库user表 将密码改为 123456

mysql> update mysql.user set password = password('123456') where user='root' ;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

可以看到修改成功了 且有生效4条,可以用以下命令看下root用户还有哪些没有设置密码
 select user,host,password from mysql.user where user='root' and password='';
Empty set (0.00 sec)
还可以看看 当前系统中有哪些用户是没有设置密码的

mysql> select user,host,password from mysql.user where password='';
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
|      | localhost |          |
|      | mysql     |          |
+------+-----------+----------+
2 rows in set (0.00 sec)

删除掉用户名为空的 账户.

mysql> drop user ''@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user where password='';
+------+-------+----------+
| user | host  | password |
+------+-------+----------+
|      | mysql |          |
+------+-------+----------+
1 row in set (0.00 sec)

mysql> drop user ''@mysql;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user where password='';
Empty set (0.00 sec)

最后的验证证明当前数据库系统没有用户为空的账号了.

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | mysql     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

最后别忘了刷新鉴权表 使配置生效
 flush privileges;

其实对于一个mysql新手来说,上述操作都过于复杂了,有没有简单的呢?答案是肯定的.mysql已经为我们准备好了一个交互式工具,通过交单的回答y/n 就可以把上边的全部搞定了.

mysql <wbr>设置root密码的方法总结mysql <wbr>设置root密码的方法总结下边的详细操作吧

[root@mysql ~]# /opt/soft/mysql/bin/mysql_secure_installation




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

# 由于在上边设置密码了,所以这部会提示你输入密码,否则的话直接就会提示你设置密码了
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.
#改变root密码
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
#删除匿名用户,就是那些通过主机名就可以访问的账号.
Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
#关于root远程访问
Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
#删除测试数据库test
Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
# 重载授权表
Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!
mysql <wbr>设置root密码的方法总结成功了 ^_^


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有