在CentOS环境下mysql如何远程连接

1、mysql权限修改

1.1 进入mysql控制台

[java] view plain copy
mysql -u root -p mysql //第一个mysql是执行命令,第二个mysql是系统数据库
如果顺利进入mysql控制台,请跳到1.2步骤。

如果出现修改密码时修改错误,比如:

[java] view plain copy
update user set password=’123456’ where user = ‘root’;
这样修改是有问题的。应该:
[java] view plain copy
update user set password=PASSWORD(‘123456’) where user=’root’;

依照上面那种修改密码,会导致的错误有:
[java] view plain copy
ERROR 1045(28000) :Access denied for user ”@’localhost’ (using password:No)

解决办法:
1.1.1 关闭mysql

[html] view plain copy
service mysqld stop
1.1.2 屏蔽权限
[html] view plain copy
mysqld_safe –skip-grant-table
1.1.3 新开一个终端(不能关闭原来终端)
[html] view plain copy
mysql -u root mysql
进入mysql后执行:
[html] view plain copy
UPDATE user SET password=PASSWORD(‘123456’) WHERE user=’root’;
[html] view plain copy
flush privileges;//记得要执行这句话,否则如果关闭先前的终端,又出现原来的错误
[html] view plain copy
exit;
1.2在mysql控制台下修改权限
[html] view plain copy
grant all privileges on . to ‘root’@’%’ identified by ‘123456’ with grant option;
[html] view plain copy
//root 是用户名,% 表示任意主机,’123456’ 指定的登录密码(这个和本地的root密码可以设置不同,互不影响)
[html] view plain copy
flush privileges; //重载系统权限
[html] view plain copy
exit;//退出mysql控制台
2.CentOS环境开放3306端口

添加规则,打开3306端口

[html] view plain copy
iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 3306 -j ACCEPT
查看规则是否生效

[html] view plain copy
iptables -L -n //或者 service iptables status
删除规则,关闭3306端口
[html] view plain copy

iptables -D INPUT -p tcp -m state –state NEW -m tcp –dport 3306 -j ACCEPT 
注意:上面使用iptables添加/删除规则都是临时的,如果需要重启也生效,就要保存修改:

[html] view plain copy
service iptables save //或者 /etc/init.d/iptables save
例外一种方式也可以实现:
[html] view plain copy
vi /etc/sysconfig/iptables //在该文件中加入下面这条规则也是可以生效的
[html] view plain copy
-A INPUT -p tcp -m state –state NEW -m tcp –dport 3306 -j ACCEPT
3.如何让mysql开机自动启动
3.1修改rc.local文件

[html] view plain copy
vi /etc/rc.d/rc.local
添加如下代码:

[html] view plain copy
/etc/rc.d/init.d/mysqld start

3.2使用chkconfig命令实现
先查看所有自动启动服务

[html] view plain copy
chkconfig –list //指定查看 chkconfig –list mysqld
如果没有添加到chkconfig列表中

[html] view plain copy
chkconfig –add mysqld
开启自动启动

[html] view plain copy
chkconfig mysqld on
查看是否启动了

[html] view plain copy
chkconfig –list mysqld
结果显示:

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表示在系统级别为:2、3、4、5时自动启动

https://blog.csdn.net/celemaple/article/details/39934775

猜你喜欢

转载自blog.csdn.net/mryangll/article/details/80450783
今日推荐