-1

I am using docker with amazonlinux2 and php already installed on docker. I am trying to connect with sequal ace(mysql database) on mac, trying to run on local. tried with 127.0.0.1 and localhost on the place of hostname here is "db"

here is my code:-

<?php
$con = new mysqli("db","apr","password","lumen_local");

if ($con->connect_errno) {
    printf("connection failed: %s\n", $con->connect_error());
    exit();
}
$res = $con->query("SELECT VERSION()");
if ($res) {
    $row = $res->fetch_row();
    echo $row[0];
}
$res->close();
$con->close();

?> 

when I am trying with the above code it is showing the following error:-

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'apr'@'amazonlinux-web-1.amazonlinux_default' (using password: YES) in /work/api/test/mysql.php:3 Stack trace: #0 /work/api/test/mysql.php(3): mysqli->__construct('db', 'apr', 'password', 'lumen_local') #1 {main} thrown in /work/api/test/mysql.php on line 3

but when i change the host name from "db" to "127.0.0.1"

with the following code:-

<?php
$con = new mysqli("127.0.0.1","apr","password","lumen_local");

if ($con->connect_errno) {
    printf("connection failed: %s\n", $con->connect_error());
    exit();
}
$res = $con->query("SELECT VERSION()");
if ($res) {
    $row = $res->fetch_row();
    echo $row[0];
}
$res->close();
$con->close();

?>

it shows the different error:-

Fatal error: Uncaught mysqli_sql_exception: Connection refused in /work/api/test/mysql.php:3 Stack trace: #0 /work/api/test/mysql.php(3): mysqli->__construct('127.0.0.1', 'apr', 'password', 'lumen_local') #1 {main} thrown in /work/api/test/mysql.php on line 3

I am looking for your support, Thanks in advance!

here is my code:-

<?php
$con = new mysqli("db","apr","password","lumen_local");

if ($con->connect_errno) {
    printf("connection failed: %s\n", $con->connect_error());
    exit();
}
$res = $con->query("SELECT VERSION()");
if ($res) {
    $row = $res->fetch_row();
    echo $row[0];
}
$res->close();
$con->close();


?> 
<?php
$con = new mysqli("127.0.0.1","apr","password","lumen_local");

if ($con->connect_errno) {
    printf("connection failed: %s\n", $con->connect_error());
    exit();
}
$res = $con->query("SELECT VERSION()");
if ($res) {
    $row = $res->fetch_row();
    echo $row[0];
}
$res->close();
$con->close();

?>
rooh
  • 1
  • 1

1 Answers1

-1
  1. Access denied for user 'apr'@'amazonlinux-web-1.amazonlinux_default'

This is caused by the apr user doesn't have the access right, you can grant the right to user apr in db server like below:

$ mysql -u root -p
Enter Password:

mysql> GRANT ALL ON lumen_local.* TO 'apr'@'amazonlinux-web-1.amazonlinux_default' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
  1. Connection refused when using localhost/127.0.0.1

This is caused by MySQL server not listen in localhost/127.0.0.1, you can check it in db server with command like below:

# netstat -nlt|grep -E 'Proto|3306'
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 192.168.0.1:3306            0.0.0.0:*           LISTEN

the Local Address column here is 192.168.0.1:3306, in your environment it should be you db server's ip address.

Xu Ouyang
  • 49
  • 1
  • Thanks for your reply! I tried it is showing Proto Recv-Q Send-Q Local Address Foreign Address (state) tcp4 0 0 127.0.0.1.3306 127.0.0.1.60428 ESTABLISHED tcp4 0 0 127.0.0.1.60428 127.0.0.1.3306 , and when i tried with $ mysql -u root -p :- it is showing command not found, I have docker in docker apache and my sql server and then made a container by this command "docker compose up -d --build". – rooh Mar 23 '23 at 01:48
  • you can try command like 'docker compose exec sh' to connect to docker instance, then run mysql command – Xu Ouyang Mar 23 '23 at 05:15
  • i tried with "docker exec -it amazonlinux-db-1 bash -p" it took me to "bash-4.4# " but not knowing what to do next? – rooh Mar 23 '23 at 07:02
  • Run the command 'mysql -u root -p' login to the database, then run the SQLs described in 1. Access denied for user 'apr'@'amazonlinux-web-1.amazonlinux_default' – Xu Ouyang Mar 24 '23 at 05:37