停止数据库如果是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;
推荐阅读
- 讲解linux清空某目录内文件 linux清空文件内容
- shell连接oracle执行sql脚本 linux连接oracle数据库命令
- 网页投票制作 怎样制作网页投票制作?
- 普通人每天回家都要对鞋底消毒吗
- linux系统详细安装步骤 服务器系统怎么安装驱动
- 血橙和普通橙哪个比较有营养 红肉橙与血橙有区别吗
- 岚图FREE、岚图梦想家双车试驾:用户型企业如何玩转技术和产品?
- 真空胎和普通胎的区别
- ~毛蛋与普通鸡蛋的区别和营养价值~
- 6藏红花的一些禁忌