title: RHEL unable to connect MySQL database via PHP code
body: <p>I have been trying to find the issue but failed to spot so far in my <code>RHEL Server 7.5(Maipo)</code>. I have a remote database instance (RDS instance) on my AWS. Which is in the same public subnet. And can be accessed and connected if I run <code>mysql -h &lt;remote-db-hostname&gt; -u &lt;username&gt; -p</code> in my above mentioned instance terminal.</p>
  
  <p>Moreover, if I try to run <code>sudo telnet &lt;remote-db-hostname&gt; 3306</code> it returns success, and says: "<em>connected to xxxx.xxx. host</em>".</p>
  
  <p><strong>Installed <code>yum</code> packages:</strong></p>
  
  <ul>
  <li><code>php-mysql.x86_64</code>  &nbsp;&nbsp;&nbsp;&nbsp; <em>(5.4.16-45.el7)</em></li>
  <li><code>mariadb.x86_64</code>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>(1:5.5.56-2.el7)</em></li>
  <li><code>mariadb-libs.x86_64</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>(1:5.5.56-2.el7)</em></li>
  <li><code>httpd.x86_64</code> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>(2.4.6-80.el7_5.1)</em></li>
  <li><code>httpd-tools.x86_64</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>(2.4.6-80.el7_5.1)</em></li>
  </ul>
  
  <p><strong>PHP connection doesn't work</strong></p>
  
  <p>But when I try to connect to connect via simple PHP code, it doesn't work. It says: 
  <em>Database connection error (2): Could not connect to MySQL.</em>
  or
  <em>Could not connect host: Can't connect to MySQL server on 'xxxx.eu-west-2.rds.amazonaws.com' (13)</em> </p>
  
  <p>I have tried to use host name both ways, with Port and without Port but no success.</p>
  
  <p><em>Test Connection File:</em></p>
  
  <pre><code>$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);
  </code></pre>
  
  <p><em>Here is my <code>/etc/my.cnf</code> file:</em></p>
  
  <pre><code>[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
  </code></pre>
  
  <p>Note: There are two files in <code>/etc/my.cnf.d</code>; 1) <code>client.cnf</code> 2) <code>mysql-clients.cnf</code></p>
  
  <p><em>client.cnf</em></p>
  
  <pre><code># 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]
  </code></pre>
  
  <p><em>mysql-clients.cnf</em></p>
  
  <pre><code># 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]
  </code></pre>
  
  <p><em>/etc/selinux/config</em>:</p>
  
  <pre><code># 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
  </code></pre>