方法1: 用SET PASSWORD命令mysql
mysql -u rootsql
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');数据库
方法2:用mysqladmin安全
mysqladmin -u root password "newpass"服务器
若是root已经设置过密码,采用以下方法socket
mysqladmin -u root password oldpass "newpass"post
方法3: 用UPDATE直接编辑user表ui
mysql -u rootthis
mysql> use mysql;spa
mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
方法4:在丢失root密码的时候,能够这样
mysqld_safe --skip-grant-tables&
mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;
--------------------------------------------------------------------------------------
下面是经过修改配置文件修改root密码:
1.首先确认服务器出于安全的状态,也就是没有人可以任意地链接MySQL数据库。
由于在从新设置MySQL的root密码的期间,MySQL数据库彻底出于没有密码保护的
状态下,其余的用户也能够任意地登陆和修改MySQL的信息。能够采用将MySQL对
外的端口封闭,而且中止Apache以及全部的用户进程的方法实现服务器的准安全
状态。最安全的状态是到服务器的Console上面操做,而且拔掉网线。
2.修改MySQL的登陆设置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
保存而且退出vi。
3.从新启动mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
4.登陆并修改MySQL的root密码
# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
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 ( 'new-password' ) WHERE User = 'root' ;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
5.将MySQL的登陆设置修改回来
# vi /etc/my.cnf
将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存而且退出vi。
6.从新启动mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
----------------------------------------------------------------------
其实想要重置 5.7 的密码很简单,就一层窗户纸:
一、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1
这一行配置让 mysqld 启动时不对密码进行验证
二、重启 mysqld 服务:systemctl restart mysqld
三、使用 root 用户登陆到 mysql:mysql -u root
四、切换到mysql数据库,更新 user 表:
update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
在以前的版本中,密码字段的字段名是 password,5.7版本改成了 authentication_string
五、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容
六、重启 mysqld 服务,再用新密码登陆便可