Mysql Database connection denied - php

I am trying to get my Symfony 3 app running in production mode on an Amazon EC2 instance.
I am have the code in the correct place, and then I try to setup any dependencies with composer by running
export SYMFONY_ENV=prod
composer install --no-dev --optimize-autoloader
but get the error
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driverL SQLSTATE[HY000] [1044] Access denied for user 'my-user'#'127.0.0.1' to database 'my_database'
This error is then passed down to [Doctrine\DBAL\Driver\PDOException] and [PDOException] before the script terminates.
This is then followed by the message
[RuntimeException]
An error occured when executing the "'assets:install --symlink --rela'\''web'\'''" command:
which is then followed by the same errors as at the beginning.
My parameters.yml file is
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: my_database
database_user: my-user
database_password: mypassword
server_version: 5.6
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: Mysecret
registration_permitted: true
registration_limit: 3
and the user my-user has all permissions on the database my_database except GRANT.
I have been trying to solve this for a few hours to no avail, other solutions I came across involved misspelt usernames/passwords/databases (triple-checked) or needed the server version adding (which hasn't made a difference).
Additional Info
Running mysql -u my-user -p prompts for the password and then allows me to log in.
Running mysql -u my-user -p -D my_database prompts for the password and then returns ERROR 1044 (42000): Access denied for user 'my-user'#'localhost' to database 'my_database'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'; returns ERROR 1141 (42000): There is no such grant defined for user 'my-user' on host '%'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'#'localhost'; returns GRANT USAGE ON *.* TO 'my-user'#'localhost' IDENTIFIED BY PASSWORD hashedPassword.
Additional Info 2
After deleting my-user ('my-user'#'localhost' and 'my-user'#'127.0.0.1') and then recreating them using GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'localhost' IDENTIFIED BY 'password'; and also GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'127.0.0.1' IDENTIFIED BY 'password'; and then FLUSH PRIVILEGES; The output of SHOW GRANTS FOR 'my-user'#'localhost' is then
GRANT USAGE ON *.* TO 'my-user'#'localhost' IDENTIFIED BY PASSWORD 'passwordHash'
GRANT ALL PRIVILEGES ON 'my_database' TO 'my-user'#'localhost'
and similar output for 'my-user'#'127.0.0.1'.
But this is still giving me the same errors as before, any more ideas?

As discussed in comments you did not have any privilege on the database.
So running this fixes your problem:
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'localhost'
About your doubt why you had to change it to localhost in your params because that's what you're telling mysql
Imagine you want to restrict database access to only a specific ip in your network you could run
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'192.168.1.10'
So replace 192.168.1.10 with 127.0.0.1 and you can use 127.0.0.1 in your parameters

Related

Mysql cannot login from user account

From root user, I can execute this command without any problems:
[root#freebsd ~/var/html/www]$ bin/console d:run-sql 'SELECT * from user';
From www user, I cannot execute this command:
[www#freebsd ~/var/html/www]$ bin/console d:run-sql 'SELECT * from user';
because I am getting the following error:
An exception occurred in driver: SQLSTATE[HY000] [2002] Permission denied
Both users are using the same config file:
parameters:
database_driver: "pdo_mysql"
database_url: "mysql://dbuser:dbuserpass#127.0.0.1:3306/dbname"
I am using MariaDB 10.4 which is running on Freebsd with PHP(Symfony project)
Authentication problems comes with an access denied message.
A permission denied error means that you authentication happened but the resource you're trying to access was denied. Probably the dbname database.
You need to grant at least a "select" permission to this user with root or another administrator user:
GRANT SELECT ON dbname.* TO dbuser#127.0.0.1;
Pay attention because with mysql client if we don't specify a host or specify it like localhost instead of 127.0.0.1, the client tries to connect trought unix socket.
You can overcome this with --protocol tcp or using a IP address.

Access denied for user 'root'#'localhost' after installing phpmyadmin on ubuntu

I installed phpmyadmin on my ubuntu server following these steps here:
https://www.rosehosting.com/blog/install-phpmyadmin-on-ubuntu-16-04/
I installed apache, php, mysql following these steps:
https://vijayasankarn.wordpress.com/2017/01/17/setting-lamp-stack-in-ubuntu-16-04-using-aws-ec2/
and when I goto login to http://myserver.com/phpmyadmin I get this error:
mysqli_real_connect(): (HY000/1045): Access denied for user
'root'#'localhost' (using password: YES)
What am I doing wrong? Am I missing a step that is not in the links above?
I have tried this:
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
and this
mysql -u root -p
use mysql;
update user set plugin="" where user='root';
flush privilege;
This error can be fixed, the following command will reconfigure your phpMyAdmin credentials to gain access to the MYSQL DB:
sudo dpkg-reconfigure phpmyadmin
/!\ This will provide few interactive menus that will allow you to reconfigure the phpMyAdmin package with NEW credentials /!\
But you could also reconfigure it manually :
1 - Log into mysql as root
mysql -u root -p
2 - Make sure 'phpmyadmin' user exists :
SELECT User FROM mysql.user;
3 - Switch to the appropriate MYSQL DB :
use phpmyadmin;
4 - Set new password for the phpmyadmin user
UPDATE user SET password=PASSWORD('yourNewPassword') WHERE User='phpmyadmin';
sudo mysql -u root -p
USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE User ='root';
FLUSH PRIVILEGES;
exit;
sudo systemctl restart mysql
Check to see that you have actually set the root password in MySQL. You might find that when logged in as the root Linux user you can run
mysql -u root -p
and get access to your database no matter which password you specify, because the password hasn't actually been set. Use
ALTER USER 'root'#'localhost' IDENTIFIED BY 'PASSWORD'; FLUSH PRIVILEGES; EXIT;
to set your password, then try phpMyAdmin again.

SQLSTATE[HY000] [1698]

How can I overcome this error ?
Error: SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select count(*) as aggregate from users
where email = email#gmail.com
I'm using Ubuntu
Please help
Login as root first:
$ sudo mysql -u root
Then CREATE or ALTER a non-root user (use '127.0.0.1' instead of 'localhost' if needed):
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'adminspassword';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost';
FLUSH PRIVILEGES;
Exit and restart:
exit
$sudo service mysql restart
$sudo service apache2 restart
And edit the .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3630
DB_DATABASE=yourdbname
DB_USERNAME=admin
DB_PASSWORD=adminspassword
MySQL will make a difference between "localhost" and "127.0.0.1".
It might be possible that 'root'#'localhost' is not allowed because there is an entry in the user table that will only allow root login from 127.0.0.1.
This could also explain why some application on your server can connect to the database and some not because there are different ways of connecting to the database. And you currently do not allow it through "localhost".
I know its late however looking for answers, I couldn't find anything and at last I got this answer.
$sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
Now you should be able to log in as root.
Thanks #Matematikisto in this thread
I encountered this problem in MySQL 8, Ubuntu 20. By default, the policy does not grant "GRANT" rights, including for root, but even after manipulations to obtain them, the application was able to gain access only after granting the mysql_native_password rights to the account. Maybe it will help someone:
UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('password'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
the full sequence of steps to change the password and its format is here:
MySQL: How to reset or change the MySQL root password?

propel:database:create access denied for user 'user'#'localhost'

For my Symfony2 project, I'm using Propel with MySQL.
When I deployed it on a server and ran:
php app/console propel:database:create --env=prod
I got this error:
[Propel] Exception caught
Unable to open PDO connection [wrapped: SQLSTATE[28000] [1045] Access denied for user 'user'#'localhost' (using password: YES)]
Which is really weird because when I run MySQL CLI with exactly the same host, user and password it works fine.
$ mysql -h localhost -u user -p
Propel-bundle: 1.2.13
What's going on?
Try these steps
Configure config/databases.yml with the working username and password. Ensure you are using the correct password and port here
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: 'mysql:dbname=your-database-name;host=localhost'
port : 3306
username: user
password : your-password
encoding: utf8
persistent: true
pooling: true
Disable SELnux: If SELinux is enabled which can block the connection. Try to disable that, although not the optimal for those that like of security on their systems

Connecting to Remote Server MySQL Issue

I'm attempting to connect to a remote server, which I'll refer to as machine A. I've created a user following the instructions here
CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
WITH GRANT OPTION;
CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
WITH GRANT OPTION;
On machine A I can run the command
mysql -u monty -h website.com -p
This connects to sql with no problem. However, when attempting to do this from some machine B I receive the error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'website.com' (113)
I've also commented out the following line:
# bind-address = 127.0.0.1
in the /etc/mysql/my.cnf file. Still no luck connecting from a remote connection. Any obvious things that I might be missing? Any feedback as always is very much appreciated.
I think it's your GRANT that needs fixing.
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
Might need to be
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'website.com'
You're going to want to make sure things are secure though.
It's usually best practice to try not to allow outside mysql connects that aren't from localhost.
It looks like the 'website.com' address cannot be resolved from the machine B. Please try to connect the MySQL server using the IP address of machine A, i.e.:
mysql -u monty -h x.x.x.x -p
If it will work, please make sure you mapped the IP address of the machine A to the name 'website.com' correctly.
My problem was that the firewall was blocking the connection.
I was using CentOS 7 and was getting this error:
mysql -usomeuser -h192.168.194.4 -p somedb
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.194.4' (113)
So, I installed telnet to try and got this:
[root#vm3 config]# telnet 192.168.194.4 3306
Trying 192.168.194.4...
telnet: connect to address 192.168.194.4: No route to host
and as others noted, the error 113 is "No route to host" which is not a MySQL config issue.
I could have just opened 3306 to the world or just the one IP I was connecting from, but instead, I decided to create a new zone since it was for my ESX host's internal "hostonly" network.
On the host running MySQL (MariaDB), I ran these firewall commands:
firewall-cmd --new-zone=esxlocalhost --permanent
firewall-cmd --reload
firewall-cmd --zone=esxlocalhost --permanent --add-source=192.168.194.0/24
firewall-cmd --zone=esxlocalhost --permanent --add-port=3306/tcp
firewall-cmd --reload
Once that was done, I could connect on the client:
mysql -usomeuser -h192.168.194.4 -p somedb
Enter password:
And life was good

Categories