RHEL无法通过PHP代码连接MySQL数据库

时间:2018-07-18 09:28:05

标签: php mysql apache amazon-web-services rhel

我一直在尝试查找问题,但到目前为止在我的RHEL Server 7.5(Maipo)中未能发现。我的AWS上有一个远程数据库实例(RDS实例)。哪个在同一公共子网中。如果我在上述实例终端中运行mysql -h <remote-db-hostname> -u <username> -p,就可以访问并连接它。

此外,如果我尝试运行sudo telnet <remote-db-hostname> 3306,它将返回成功,并说:“ 已连接到xxxx.xxx。主机”。

已安装yum个软件包:

  • php-mysql.x86_64 (5.4.16-45.el7)
  • mariadb.x86_64 (1:5.5.56-2.el7)
  • mariadb-libs.x86_64 (1:5.5.56-2.el7)
  • httpd.x86_64 (2.4.6-80.el7_5.1)
  • httpd-tools.x86_64 (2.4.6-80.el7_5.1)

PHP连接无效

但是当我尝试通过简单的PHP代码进行连接时,它不起作用。它说: 数据库连接错误(2):无法连接到MySQL。 要么 无法连接主机:无法连接到“ xxxx.eu-west-2.rds.amazonaws.com”(13)上的MySQL服务器

我试图同时使用主机名和端口,没有端口但没有成功。

测试连接文件:

$host = 'remote-db-hostname:3306';  // ofcourse using correct hostname
$user = 'xxxxx';
$pswd = 'xxxxx';

$link = mysql_connect($host, $user, $pswd);
if (!$link) {
    die('Could not connect host: ' . mysql_error());
}
mysql_select_db('my-db-name', $link) or die('could not connect to the specified database');

mysql_close($link);

这是我的/etc/my.cnf文件:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

注意:/etc/my.cnf.d中有两个文件; 1)client.cnf 2)mysql-clients.cnf

client.cnf

# These two groups are read by the client library
# Use it for options that affect all clients, but not the server

[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]

mysql-clients.cnf

# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]

/ etc / selinux / config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

0 个答案:

没有答案