Mysql远程连接访问的问题处理

JAVA记事 2019-06-26

今天在升级阿里云服务器MySQL从 5.5到5.7版本时,升级成功后,不能远程访问了,只能在服务器终端访问,折腾了一下午,原来是MySQL配置my.cnf 里边的[mysqld] bind-address=127.0.0.1 限制的问题,注释掉就可以了

一、事故现场

使用PHP

<?php 
 // 1.连接数据库
$db_host ="121.29.19.5:3306";
$db_user = "root";
$db_pass = "ali_123456";

  $conn = mysql_connect($db_host,  $db_user, $db_pass);
  if (!$conn)
  {
    die("Could not connect:" . mysql_error());
  }else
{
   dump("连接成功!");

}

错误抛出:

Could not connect:Lost connection to MySQL server at 'reading initial communication packet', system error: 111

什么原因呢?后来在网上找了一圈,原来是MySQL配置bind-address限制的问题。

MySQL配置修改

先找到 my.conf 配置文件

find / -name my.conf
cat my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

最终找到[mysqld]mysql.conf.d文件夹中:

注释掉即可:bind-address=127.0.0.1

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1  
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

相关推荐