Mysql初始化root密码和允许远程访问

mysql默认root用户没有密码,输入 mysql –u root 进入mysql

初始化root密码

mysql > update user set password=PASSWORD('123456') where User='root';

允许mysql远程访问

可以使用以下三种方式:

1 改表

mysql -u root –p;
mysql > use mysql;
mysql > update user set host = '%' where user = 'root';
mysql > flush privileges;

2 授权

例如,你想root使用123456密码从任何主机连接到mysql服务器:

mysql > grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql > flush privileges;

如果你想允许用户jack从ip为10.10.50.127的主机连接到mysql服务器,并使用654321作为密码:

mysql > grant all privileges on *.* to 'jack'@'10.10.50.127' identified by '654321' with grant option;
mysql > flush privileges;

3 在安装mysql的机器上运行

mysql -h localhost -u root
mysql > grant all privileges on *.* to 'root'@'%' with grant option;
mysql > flush privileges;
mysql > exit;

如果还是不能远程登录,那你就检查一下防火墙设置了,看看 MySQL 端口有没有对外开放。