Skip to content
Advertisement

Server select – MySQL / MariaDB

I have two database servers, MySQL and MariaDB, when I’m trying to connect to my local database on MySQL server, I always get “Connectionfailed: SQLSTATE[HY000] [1049] Unknown database ‘ruff'”. I found out, that my code is connecting into MariaDB server instead of MySQL. I can CREATE and CONNECT to any database on MariaDB.

How can I connect to MySQL server databases?

My code is:

// Creating a connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// Creating a database named $dBName
$sql = "CREATE DATABASE $dBName";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully with the name newDB";
} else {
    echo "Error creating database: " . $conn->error;
}
// Connecting to database named $dBName
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn)
{
    die("Connection fail: ".mysqli_connect_error());
}

// closing connection
$conn->close();

Advertisement

Answer

Adding port number after the localhost solved the problem.

$servername = "localhost:3308"; //MySQL
$servername2 = "localhost:3306"; //MariaDB
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement