MySQL修改root账号密码的方法

所属分类: 数据库 / Mysql 阅读数: 307
收藏 0 赞 0 分享

MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法

1: 使用set password语句修改

mysql> select user();
+----------------+
| user()  |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.08 sec)
 
mysql> set password=password('123456');
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit

2: 更新mysql数据库的user表

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> update user set password=PASSWORD('QwE123') where user='root';
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4 Changed: 4 Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> quit

3:使用mysqladmin命令修改

命令一般为 mysqladmin -u root -p'oldpassword' password newpass 如下所示:

[root@DB-Server ~]# mysqladmin -u root -p'123456' password 'Qwe123'
Warning: Using a password on the command line interface can be insecure.

验证root密码修改是否成功

[root@DB-Server ~]# mysqladmin -u root -p'123456' password 'Qwe123'
Warning: Using a password on the command line interface can be insecure.


 

上面都是在知道root密码的情况下修改root密码,如果忘记了root密码,如何修改root的密码呢?

1:首先停掉MySQL服务

[root@DB-Server ~]# service mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server ~]# 


[root@DB-Server ~]# /etc/rc.d/init.d/mysql stop
Shutting down MySQL..[ OK ]


2:然后使用mysqld_safe命令启动mysql,更新root账号的密码

    --skip-grant-tables:不启动grant-tables(授权表),跳过权限控制。

    --skip-networking :跳过TCP/IP协议,只在本机访问(这个选项不是必须的。可以不用)是可以不用

[root@DB-Server ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[1] 5145
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# 150709 14:10:53 mysqld_safe Logging to '/var/lib/mysql/DB-Server.localdomain.err'.
150709 14:10:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
 
[root@DB-Server ~]# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
 
Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database  |
+--------------------+
| information_schema |
| mysql  |
| performance_schema |
| test  |
+--------------------+
4 rows in set (0.00 sec)
 
mysql> use mysql
Database changed
mysql> UPDATE user SET password=PASSWORD("Qwe123") WHERE user='root'; 
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
Bye

3:重新启动MySQL服务。

以上所述就是本文的全部内容了,希望大家能够喜欢

更多精彩内容其他人还在看

MySQL 创建索引(Create Index)的方法和语法结构及例子

MySQL 创建索引(Create Index)的方法和语法结构及例子
收藏 0 赞 0 分享

MYSQL 优化常用方法

如何优化你的MYSQL呢?请看下面:(不错的优化方案)
收藏 0 赞 0 分享

linux mysql 找回密码

linux下mysql忘记密码的解决方法。
收藏 0 赞 0 分享

mysql 动态执行存储过程语句

MSSQL中动态执行sql语句可以使用EXEC()函数。MSSQL中也有类似的函数EXECUTE(),不过不同的是MYSQL中动态执行存储过程语句与MSSQL还是有区别的。
收藏 0 赞 0 分享

MySQL 查找价格最高的图书经销商的几种SQL语句

不同的图书,在不同的经销商的价格不同,我们这里要找到每种图书最高的经销商是谁? 找最低的类似了。
收藏 0 赞 0 分享

MySQL 客户端不输入用户名和密码直接连接数据库的2个方法

MySQL 客户端不输入用户名和密码直接连接数据库的2个方法,大家可以测试下。
收藏 0 赞 0 分享

mysql 查询表中平均分最低的班级

查询出dd(user_id,class_no,score)这个表中平均分最低的班级?
收藏 0 赞 0 分享

mysql 左连接、右连接和内连接

以MySql为例。在MySQL数据库中建立两张数据表,并分别插入一些数据。
收藏 0 赞 0 分享

Mysql LONGBLOB 类型存储二进制数据 (修改+调试+整理)

代码来自网络,我学习整理了一下,测试通过,下面的参数需要设置为你自己的
收藏 0 赞 0 分享

Mysql LONGTEXT 类型存储大文件(二进制也可以) (修改+调试+整理)

MySql2.cpp : Defines the entry point for the console application.
收藏 0 赞 0 分享
查看更多