CentOS 7 MySQL5.7 密码处理

忘记密码处理

忘记mysql的密码,可按如下5步操作,可重新设置密码。当然,我喜欢在新安装数据库后,用如下方式设置密码,不用输入系统自动生成的密码。

mysqladmin -uroot -p password 123
select user,authentication_string,host from mysql.user;
#
1.停数据库 /etc/init.d/mysqld stop # 2.启动数据库为无密码验证模式 mysqld_safe --skip-grant-tables --skip-networking &
#3.登录mysql并修改密码
mysql
update mysql.user set authentication_string=PASSWORD('123456') where user='root';
exit
#4.启动数据库
/etc/init.d/mysqld restart
#5.新密码登录
mysql -uroot -p123

查看初始密码

#方式一
more /var/log/mysqld.log
#方式二
grep "temporary password" /var/log/mysqld.log 

修改密码

1.不需要登录MYSQL

mysqladmin -u root -p password "新密码1234"
Enter password: 【输入原来的密码】

2.需要登录到MYSQL 

msql -u root -p
endter password:输入密码

#方式一
SET PASSWORD = PASSWORD('新密码1234');

#方式二
alter user user() identified by '新密码1234';

3.用mysql本地登录

 update user set authentication_string=password('123456') where user='root' ;
 flush privileges;

开启远程访问

MySql-Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问.

 #登录数据库
mysql -u root -p
use mysql;

#查询用户列表
select  User,authentication_string,Host from user;

#新增用户及其权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456'  with grant option;
#这里的123456为你给新增权限用户设置的密码,%代表所有主机,也可以具体到你的主机ip地址

 flush privileges;        
 #这一步一定要做,不然无法成功! 这句表示从mysql数据库的grant表中重新加载权限数据

 #因为MySQL把权限都放在了cache中,所以在做完更改后需要重新加载。

#删除用户及其权限
delete from user where Host='%';
 

  

mysql开放端口

默认情况下,防火墙是拦截3306端口的,无法远程登录数据库,需要手动开放端口或关闭防火墙

#添加mysql端口3306和Tomcat端口8080
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload

原文地址:https://www.cnblogs.com/ypeuee/p/13501761.html