linux修改root密码命令(linux普通用户修改root密码)

2022-10-22,,,,

停止数据库

如果是apt-get或yum安装的mysql执行:

systemctl stop mysqld

修改my.cnf

利用vim命令打开mysql配置文件my.cnf,在mysqld进程配置文件中添加skip-grant-tables,添加完成后,执行wd保存。

vi /etc/my.cnf
[mysqld]
skip-grant-tables

重启数据库

利用命令行工具重启数据库

systemctl start mysqld

修改root密码

重启数据库后可以不用密码直接回车登陆:

./mysql -p enter password:

可以直接登陆进数据库。

在mysql命令行下执行以下命令修改root密码:

update mysql.user set password=password('newpassword') where user='root'

#将password()中的newpassword字符更改为你自己的密码

mysql5.7及以上版本注意

当使用update user set passsword=password(“123456″) where user=”root”;修改root密码时会提示如下错误:

error 1054 (42s22): unknown column 'passsword' in 'field list'

这是因为在mysql 5.7 password字段已从mysql.user表中删除,新的字段名是“authenticalion_string”.

可使用use mysql; desc user;查看如下:

+------------------------+-----------------------------------+------+-----+-----------------------+-------+ | field | type | null | key | default | extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | host | char(60) | no | pri | | | | user | char(32) | no | pri | | | | select_priv | enum('n','y') | no | | n | | | insert_priv | enum('n','y') | no | | n | | | update_priv | enum('n','y') | no | | n | | | delete_priv | enum('n','y') | no | | n | | | create_priv | enum('n','y') | no | | n | | | drop_priv | enum('n','y') | no | | n | | | reload_priv | enum('n','y') | no | | n | | | shutdown_priv | enum('n','y') | no | | n | | | process_priv | enum('n','y') | no | | n | | | file_priv | enum('n','y') | no | | n | | | grant_priv | enum('n','y') | no | | n | | | references_priv | enum('n','y') | no | | n | | | index_priv | enum('n','y') | no | | n | | | alter_priv | enum('n','y') | no | | n | | | show_db_priv | enum('n','y') | no | | n | | | super_priv | enum('n','y') | no | | n | | | create_tmp_table_priv | enum('n','y') | no | | n | | | lock_tables_priv | enum('n','y') | no | | n | | | execute_priv | enum('n','y') | no | | n | | | repl_slave_priv | enum('n','y') | no | | n | | | repl_client_priv | enum('n','y') | no | | n | | | create_view_priv | enum('n','y') | no | | n | | | show_view_priv | enum('n','y') | no | | n | | | create_routine_priv | enum('n','y') | no | | n | | | alter_routine_priv | enum('n','y') | no | | n | | | create_user_priv | enum('n','y') | no | | n | | | event_priv | enum('n','y') | no | | n | | | trigger_priv | enum('n','y') | no | | n | | | create_tablespace_priv | enum('n','y') | no | | n | | | ssl_type | enum('','any','x509','specified') | no | | | | | ssl_cipher | blob | no | | null | | | x509_issuer | blob | no | | null | | | x509_subject | blob | no | | null | | | max_questions | int(11) unsigned | no | | 0 | | | max_updates | int(11) unsigned | no | | 0 | | | max_connections | int(11) unsigned | no | | 0 | | | max_user_connections | int(11) unsigned | no | | 0 | | | plugin | char(64) | no | | mysql_native_password | | | authentication_string | text | yes | | null | | | password_expired | enum('n','y') | no | | n | | | password_last_changed | timestamp | yes | | null | | | password_lifetime | smallint(5) unsigned | yes | | null | | | account_locked | enum('n','y') | no | | n | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ 45 rows in set (0.00 sec)

上面表中authentication_string的就是新的密码字段。使用上面的字段就可以重置密码 :

mysql> update user set authentication_string=password('123456') where user="root"; 
query ok, 1 row affected, 1 warning (0.00 sec) rows matched: 1 changed: 1 warnings: 1 
mysql> flush priviledges;

重启数据库

密码修改完成后,将my.cnf文件中添加的skip-grant-tables语句注释或删除掉,然后重启数据库即可

登陆检测

使用新密码重新登陆测试

《linux修改root密码命令(linux普通用户修改root密码).doc》

下载本文的Word格式文档,以方便收藏与打印。