0

I am trying to connect a CRUD Simple with PHP, connecting a database in MySQL. The problem is that in the mysql_connect function, in the host variable, if I pass the String localhost, it connects and shows me the information of the database. The problem is when I put the IP of the host where the MySQL database is located, it shows me Permission denied. I already send the privileges to the user carlos with the following command:

GRANT ALL PRIVILEGES ON * . * TO 'carlos'@'%';

This is the connection code, the rest of the code does not affect, since this set are the first lines of the program:

$link = mysqli_connect("192.168.0.18:3306", "carlos", "abc123", "distribuidos") or die ('Error. ' . mysqli_connect_error());

With localhost, the connection work:

$link = mysqli_connect("localhost", "carlos", "abc123", "distribuidos") or die ('Error. ' . mysqli_connect_error());

It should be noted that I have also configured the firewall with the ports already open, in this case 3306, and if I connect with the command mysql -u carlos -h 192.168.0.18 -p from another machine connects me without problem.

I am using php 7.3 and CentOS 7.

SOLUTION

My solution is execute the next command on Linux: sudo setsebool -P httpd_can_network_connect 1

Garnica1999
  • 215
  • 2
  • 12

1 Answers1

1

Three things you can check

  1. Please check your port is open or not

or

  1. Remove bind-address 127.0.0.1 in /etc/mysql/my.cnf

or

  1. apache is not configured for external access. try sudo setsebool -P httpd_can_network_connect 1
snfrox
  • 124
  • 7
  • is this your mysql server ip address. if so it's wrong, you can either put 0.0.0.0 to allow any network or the sub net address to specifically give access to a network – snfrox Mar 07 '19 at 06:15
  • read this for more details about bind address https://stackoverflow.com/questions/3552680/bind-address-and-mysql-server – snfrox Mar 07 '19 at 06:16
  • Now change the IP to 0.0.0.0, But still with the same error, I do not know what to do anymore – Garnica1999 Mar 07 '19 at 06:20
  • did you restart the service? – snfrox Mar 07 '19 at 06:21
  • Yes, i restart the mysqld and httpd services, but the mysql connection not work. – Garnica1999 Mar 07 '19 at 06:27
  • remove port from "192.168.0.18:3306" and add it after schema name. see below post https://www.w3schools.com/php/func_mysqli_connect.asp – snfrox Mar 07 '19 at 06:42
  • It has remained as follows: `mysqli_connect("192.168.0.18", "carlos", "abc123", "distribuidos", "3306") or die ('Error. ' . mysqli_connect_error());` but it still does not work – Garnica1999 Mar 07 '19 at 06:57
  • Can you post the exact mysql or php error you are getting from server? – snfrox Mar 07 '19 at 07:00
  • `Warning: mysqli_connect(): (HY000/2002): Permission denied in /var/www/html/taller2/consultar.php on line 28`. The line 28 is `mysqli_connect("192.168.0.18", "carlos", "abc123", "distribuidos", "3306") or die ('Error. ' . mysqli_connect_error());` – Garnica1999 Mar 07 '19 at 07:03
  • try this sudo setsebool -P httpd_can_network_connect 1 – snfrox Mar 07 '19 at 07:13
  • Finally bro! Thank you. One last favor, can you explain to me what that command does? – Garnica1999 Mar 07 '19 at 07:24
  • your apache was not configured for external access. That comman enable remote access to your apache. Glad to hear your problem is resolved. Please mark this answer as accepted. I'll update my answer :) – snfrox Mar 07 '19 at 07:27