rhel无法通过php代码连接mysql数据库

hrysbysz  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(275)

我一直在努力寻找这个问题,但到目前为止在我的工作中还没有发现 RHEL Server 7.5(Maipo) . 我的aws上有一个远程数据库示例(rds示例)。在同一公共子网中。如果我运行 mysql -h <remote-db-hostname> -u <username> -p 在我上面提到的终端示例中。
而且,如果我想跑 sudo telnet <remote-db-hostname> 3306 它返回success,并说:“连接到x.。“主机”。
安装 yum Package : 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。或无法连接主机:无法连接到'x.eu-west-2.rds.amazonaws.com'上的mysql服务器(13)
我试着用主机名的两种方式,有端口和没有端口,但没有成功。
测试连接文件:

$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 客户端.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-客户端.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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题