Welcome Guest, Not a member yet? Register   Sign In
CI4 + Lando (docker) - Main connection [MySQLi]: No such file or directory
#1

(This post was last modified: 07-16-2021, 03:31 AM by CCAldo. Edit Reason: Did check connection, and inserted new item )

I'm having some issues setting up CodeIgniter 4 with Lando.
CI4 is unable to connect to the MySQL database.

Code:
"Unable to connect to the database.
Main connection [MySQLi]: No such file or directory"


CI3 has no issues with this matter.

Lando uses a lamp database with these settings

Code:
host: 'database', port: '3306', database: 'lamp', password: 'lamp', user: 'lamp'

The CI4 database.php has been changes with this:

Code:
public $lando = [
'DSN'      => '',
'hostname' => 'database',
'username' => 'lamp',
'password' => 'lamp',
'database' => 'lamp',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'     => 3306,
];
$this->defaultGroup = 'lando';


``mysql status`` shows me that UNIX socket is located at this location:
Code:
/opt/bitnami/mysql/tmp/mysql.sock

So I added this line to php.ini
Code:
; MySQLi
mysqli.default_socket = /opt/bitnami/mysql/tmp/mysql.sock

But the issue persist.

My docker container:
[Image: pMpIhsq.png]

EDIT:
When checking connection with below code the result is 'Connected succesfully'.
And inserted new news-item also trows no error

Code:
// Check connection
if ($conn->connect_error) {
dd("Connection failed: " . $conn->connect_error);
}
dump("Connected successfully");

$sql = "INSERT INTO news (news_title, news_slug, news_description) VALUES ('Title', 'title', 'Lorem ipsum')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
dump("New record created successfully. Last inserted ID is: " . $last_id);
} else {
dd("Error:", $sql, mysqli_error($conn));
}


Does anyone having the same issue?
Or is Ci4 not able to work with MySQL in docker containers?
Reply
#2

Try changing the host name to either 'localhost' or '127.0.0.1'
Reply
#3

(07-16-2021, 08:28 AM)paulbalandan Wrote: Try  changing the host name to either 'localhost' or '127.0.0.1'
No solution using 127.0.0.1
Lando makes his own hostname, which defaults to 'database'


This does work:
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{

 public function 
index()
 {
 
$this->testDatabase('database''lamp''lamp''lamp');
 }

 private function 
testDatabase(?string $hostname null, ?string $username null, ?string $password null, ?string $database null)
 {
 
$conn mysqli_connect($hostname$username$password$database);

 
// Check connection
 
if ($conn->connect_error) {
 
var_dump("Connection failed: " $conn->connect_error);
 die();
 }
 
var_dump("Connected successfully");

 
// Check insert
 
$sql "INSERT INTO news (news_title, news_slug, news_description) VALUES ('Title', 'title', 'Lorem ipsum')";
 if (
mysqli_query($conn$sql)) {
 
$last_id mysqli_insert_id($conn);
 
var_dump("New record created successfully. Last inserted ID is: " $last_id);
 } else {
 
var_dump(mysqli_error($conn));
 die();
 }
 }


But this does not;
This is the error message: Unable to connect to the database. Main connection [MySQLi]: Connection refused
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{

 public function 
index()
 {
 
$db = \Config\Database::connect();
 if (
$db->connect_error) {
 
var_dump("Connection failed: " $db->connect_error);
 die();
 }
 echo 
'connect success';

 
$news $db->query('SELECT news_id, news_slug, news_title FROM news')->getResult();
 
var_dump($news);

 } 

Reply




Theme © iAndrew 2016 - Forum software by © MyBB