MySQL修改root账号密码的方法
程序员文章站
2024-02-28 14:34:52
mysql数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法
1: 使用set password语句修改
mysql> sel...
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服务。
以上所述就是本文的全部内容了,希望大家能够喜欢