Giving permissions to user doesn’t work in MySQL - php

I executed this in phpMyAdmin.
I executed these lines:
CREATE USER root IDENTIFIED BY PASSWORD '';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
I get this when trying to connect:
Connect failed: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
$dsn = 'mysql:dbname=one';
$user = 'root';
$password = '';
$pdo = "Not set";
try{
$pdo=new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch(Exception $ex){
echo "Connect failed: " . $ex->getMessage();
}
Note that the connection above used to work..now it doesn’t (since I switched to XAMPP).

Are you sure that the root user doesn't have a password? Do you have PhpMyAdmin installed to check this?
If the problem isn't the privileges, then it's probably the port. The default port of MySQL is 3306, so if your port for MySQL is now 82 that you have to add the port in the connection.

Please, check create user specification, seems you should use another syntax for account name
Syntax for account names is 'user_name'#'host_name'.
So, something like CREATE USER 'root'#'localhost' IDENTIFIED BY PASSWORD '';.
In your case CREATE USER 'root'#'%'; but I really did not recommend use '%' for root user.

Are you flushing privileges after altering permissions?
You should execute FLUSH PRIVILEGES; to tell the server to reload the grant tables.

The host % does not apply to localhost. You need to grand permissions for localhost separately.

Related

Database connection error Access denied for user 'root'#'localhost' (using password: NO)

Whenver i go to locallhost/Blog/app/DB i encounter this error. I have tried so many times to connect to the database but could not connect. Even if i use my mysql password which is "root" this does not work.Please someone help me Here is my connection code:
host = "localhost";
$username = "root";
$password ="";
$db_name ="blog";
$conn = new mysqli($host, $username, $password, $db_name);
if($conn->connect_error){
die(' Database connection error '.$conn->connect_error);
}
This is my phpmyadmin user account:
As Indra said, avoid using root. This account should only be used by a human, even in a development environment. If you need an account that has complete and total access to everything, create one:
CREATE USER 'indra'#'localhost' IDENTIFIED WITH mysql_native_password BY 'superSecretPassword!123';
GRANT ALL ON *.* TO 'indra'#'localhost' WITH GRANT OPTION;
The root account in MySQL should only be used when you are configuring replication or fixing something what went really, really wrong 🤐

Connecting database in another server

For a web project we have been provided two servers, one for web and another for database. When i try to connecting to the database in another server i get "Cannot connect permission denied" error. I wanted to know if i have to change any settings or modify any files like php.ini or cnf etc for it to work? I am running php 7.2 on rhel 7.5 servers. Also i changed root to allow all hosts(%) in users.
$db_host = '172.xx.xx.xx';
$db_user = 'root';
$db_pass = 'passwrd';
$db_database = 'some_db';
/* End config */
$db_conx = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
You need to ensure that the "root" user has admin privileges or try creating a new user and grant it full admin right privileges.
% mysql --user=root mysql
CREATE USER 'youruser'#'yourserver' IDENTIFIED BY 'yourpass';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'#'yourserver' WITH GRANT OPTION;
FLUSH PRIVILEGES;
By default you cannot access mysql from remote ip address. First of all, you need to grant all access to root,
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Then you need to unbind the ip address part in mysql ini file, which should look like bind-address = 127.0.0.1 , either delete it or comment it by #bind-address = 127.0.0.1.
After changing all these, restart mysql service to take effect of changes.
Note: THe above location is for windows environment, if you are in linux, then likely location may be /etc/mysql/my.cnf, some cases it may be in /etc/mysql/mysql.conf.d/mysqld.cnf.

Use local network for DB connection

We have a dedicated server with proxmox on OVH. The idea is to connect the containers locally but also trough internet. So far I have 2 containers.
I added a bridge network for local IP and that is working since I am able to ping the containers from each other.
Also added bind-address=192.168.1.3 to my.cnf.
1 container is running apache + php 7.2 (192.168.1.3)
The other container is running MySQL. (192.168.1.2)
Problem
My MySQL keeps saying SQLSTATE[HY000] [2002] Connection timed out
Here is my php code:
<?php
/**
* Configuration for database connection
*
*/
$host = "192.168.1.2";
$username = "root";
$password = "root";
$dbname = "test";
$dsn = "mysql:host=$host;dbname=$dbname";
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try
{
$connection = new PDO("mysql:host=$host", $username, $password, $options);
$sql = file_get_contents("data/init.sql");
$connection->exec($sql);
echo "Database and table users created successfully.";
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
From my understanding the code is correct so it must be something with my mysql configuration.
I'm sure that is something really simple but I'm losing to much time with this.
Try to telnet 192.168.1.2 3306 from the Apache, PHP container. Can you connect?
Ensure the listening port for MySQL is 3306, if other then adjust the PHP code accordingly. Also ensure that iptables is not blocking any incoming connections. Also ensure you have correct permissions for the root and any other users you need to have permissions from other hosts.
Also, please check when making any config changes to MySQL, that you restart the service.
After searching around for a while. Found out that I had to create a new user and grant permissions to it
mysql> CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
Also on my server config /etc/mysql/mariadb.conf.d/50-server.conf I had to comment the
bind-address = 192.168.1.3
into
#bind-address = 192.168.1.3
Then restart mysql server and change my mysql details on my php code with username 'monty' and password 'some_pass'

permission denied when logging in to mysql, but able to login from cmd digitalocean

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

MySQL password not working from PHP

This is really strange, for my user root I've set the password to:
roboasd231
I can access the MySQL server from Sequel Pro just fine, however when i try to login from PHP using the exact same details i get this error:
Access denied for user 'root'#'localhost' (using password: YES)
Any ideas how to resolve this?
This is my php code:
$con = mysql_connect("localhost","root","roboasd231");
if (!$con)
{
die('Cant connect: ' . mysql_error());
}
Screen of my users table in mysql db
You mosto likely need to enable root access from a remote workstation ...
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'roboasd231';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY 'roboasd231';
FLUSH PRIVILEGES;
If the problem is connecting from localhost is because '%' doesn't include localhost access, try to use the net interface IP, for example (192.168.1.1 or whatever else you have).
Otherwise you can add the granting even to localhost, as showed above in the code section of this answer.

Categories