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

来源:互联网 发布:论文盒子软件好用吗 编辑:程序博客网 时间:2024/05/22 12:50

1、mysql权限修改

1.1 进入mysql控制台

mysql -u root -p mysql  //<span style="white-space: pre;">第一个</span>mysql是执行命令,第二个mysql是系统数据库

如果顺利进入mysql控制台,请跳到1.2步骤。

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

update user set password='123456' where user = 'root';
这样修改是有问题的。应该:

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

依照上面那种修改密码,会导致的错误有:

ERROR 1045(28000) :Access denied for user ''@'localhost' (using password:No)


解决办法:

1.1.1 关闭mysql

service mysqld stop
1.1.2 屏蔽权限

mysqld_safe --skip-grant-table
1.1.3 新开一个终端(不能关闭原来终端)

mysql -u root mysql
进入mysql后执行:

UPDATE user SET password=PASSWORD('123456') WHERE user='root';
flush privileges;//记得要执行这句话,否则如果关闭先前的终端,又出现原来的错误
exit;
1.2在mysql控制台下修改权限

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
//root 是用户名,% 表示任意主机,'123456' 指定的登录密码(这个和本地的root密码可以设置不同,互不影响)
flush privileges; //重载系统权限
exit;//退出mysql控制台

2.CentOS环境开放3306端口

添加规则,打开3306端口

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

查看规则是否生效

iptables -L -n  //或者 service iptables status
删除规则,关闭3306端口

</pre><pre name="code" class="html">iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

注意:上面使用iptables添加/删除规则都是临时的,如果需要重启也生效,就要保存修改:

service iptables save //或者 /etc/init.d/iptables save
例外一种方式也可以实现:

vi /etc/sysconfig/iptables //在该文件中加入下面这条规则也是可以生效的
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
3.如何让mysql开机自动启动

3.1修改rc.local文件

vi /etc/rc.d/rc.local
添加如下代码:

/etc/rc.d/init.d/mysqld start


3.2使用chkconfig命令实现

先查看所有自动启动服务

chkconfig --list //指定查看 chkconfig --list mysqld
如果没有添加到chkconfig列表中

chkconfig --add mysqld
开启自动启动

chkconfig mysqld on
查看是否启动了

chkconfig --list mysqld

结果显示:

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

表示在系统级别为:2、3、4、5时自动启动

1 0