MySQL初始化密码
# 停止mysql服务
service mysql stop
cd %mysql_home%/bin
# 取消密码登录
mysqld_safe --skip-grant-tables --skip-networking &
# 进行mysql交互式窗口
mysql -u root
#使用SQL语句修改密码
update mysql.user set password=password('yourpass') where user='root' and Host = 'localhost';
#停止mysqld_safe
ps aux | grep -v grep | grep mysqld_safe | awk '{print $2}' | xargs kill -9
# 启动mysql服务
service mysql start
MySQL远程登录失败
网络原因
首先判断网络是否通畅,若不通畅可能是防火墙等原因
telnet ip port | grep connect
nc -v ip port && echo ok
权限设置原因
参考:https://blog.csdn.net/lsmrsun/article/details/53140448
直接将root的 “localhost” 修改为 “%”, 允许所有IP使用root登录,这样是不安全的
use mysql
update user set host = '%' where user ='root';
给予指定IP远程root(或者自建账号)全部权限,也不是很安全。
#此处的 *.* 代码所有数据库的所有表
grant all on *.* to root@"10.1.1.1" Identified by "password";
flush privileges
给予远程root(或者自建账号)部分权限,较安全。
#此处的 *.* 代码所有数据库的所有表
grant select,insert,update,delete on *.* to root@"%" Identified by "password";
#刷新权限,无需重启
flush privileges
MySQL常见错误
Question
You must reset your password using ALTER USER statement before executing this statement.
Answer
SET PASSWORD = PASSWORD('your new password');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
flush privileges;