I have created a droplet with digitalocean and installed mariadb
using the apt install mariadb-server
command. Now I want to connect to my server using php
and I use this:
mysqli_connect('localhost', 'root', '');
The problem is I always have Access denied for user 'root'@'localhost'
error. I don't understand why this happens. I can login using mysql -uroot
when I am logged into my server. Also when I create a new user using
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
GRANT ALL ON *.* TO 'test'@'localhost';
I can connect using php mysqli_connect
. What am I doing wrong? Why can't I connect with root
?
To connect this with server you need to create a database user first, and after creating user associate it with your database then the error will go away. hope this work for you. :) Goto MySQL Database > Create new user > Add the user with the database you want to use Like this: Check this for have a look how it looks like > Then simply use this mysqli_connect("host", "your_created_user", "your_user_password", "DB_name");
Try this script
<?php
$con = mysqli_connect("localhost","test","test","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
To create User
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
GRANT ALL PRIVILEGES ON mydb.* TO 'test'@'%' WITH GRANT OPTIO