Can't connect to MySQL with a new MySQL user - php

I work on a personal website. I just put it on the production server. When I am in the development environment I connect to MySQL with root but now I just created a user specific for this website. So I wrote this command line in MySQL:
GRANT USAGE ON portefolio.* TO 'userPortefolio'#'localhost' IDENTIFIED BY PASSWORD 'myPasswordCrypted'
The query is ok, but on my website, when I try to access a page with a query, Symfony gives me that error:
SQLSTATE[42000] [1044] Access denied for user 'userPortefolio'#'localhost' to database 'portefolio'
I tried to restart the server to be sure everything is ok but nothing.
I encrypted the password with the MySQL function
password('my password not encrypt')
and in Symfony I wrote the password in non-encrypted format. Is that ok?

GRANT USAGE ON portefolio.* TO 'userPortefolio'#'localhost' IDENTIFIED BY PASSWORD 'myPasswordCrypted'
Your password should not be encrypted in this statement. MySQL crypts it automatically.

Assuming no quirks in connecting etc were made you should do a
flush privileges;
after running the grant query.

quoting from the manual: The USAGE privilege specifier stands for “no privileges.”
so everything seems to be working as designed.

Related

'root' # 'localhost' denied access to database 'mysql' even though I have already logged into phpmyadmin using the root username and password

I am using HostGator and have logged into PHPMyAdmin, but am having issues upload a .sql file from a localhost to the new database I created. The error that comes up is this:
So I decided to go back and check my privileges for my root user. I type in SELECT user () which comes back with ajohnson#localhost. I also do SELECT current_user () just to be safe and it still comes back with ajohnson#localhost. Then I type in SHOW GRANTS FOR 'root'#'localhost'; and it comes back with this error message:
which is the exact same error I get when I try and import my .sql file. I have tried changing the import file from this
Database: wordpress-test
CREATE DATABASE IF NOT EXISTS `wordpress-test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `wordpress-test`;`
to this Database: ajohnson
CREATE DATABASE IF NOT EXISTS `ajohnson` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `ajohnson`;`
I also tried typing this into my command line grant all privileges on wp.* to user#localhost identified by 'PASSWORD'; flush privileges; \q, but I got the same error message.
I have tried changing, commenting out, and removing these areas of my file completely, as well as creating new databases, but all are giving me the same error.
First you can check if you have access to create multiple database: as suggested in this post
importing a database into phpmyadmin #1044 - Access denied for user

Establish PDO MySQL connection without password (password set in [client] section in MySQL config)

What I'm trying to achieve is allow connection to MySQL server without defining password in PDO connection, instead of it I would like to define it in MySQL configuration.
I already created file /etc/mysql/conf.d/nopass.cnf with content:
[client]
host = localhost
user = someuser
password = somepass
socket = /var/run/mysqld/mysqld.sock
When connecting from shell it works as expected:
$ mysql -u someuser
mysql>
However, I'm not able to connect via PHP:
new PDO('mysql:host=127.0.0.1:3306;charset=utf8;dbname=somedb', 'someuser');
new PDO('mysql:host=127.0.0.1:3306;charset=utf8;dbname=somedb', 'someuser', null);
new PDO('mysql:host=127.0.0.1:3306;charset=utf8;dbname=somedb', 'someuser', );
In each case I receive error
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'someuser'#'localhost' (using password: NO)
It works if I set correct password
new PDO('mysql:host=127.0.0.1:3306;charset=utf8;dbname=somedb', 'someuser', 'somepassword');
Is it possible to make this work?
As far as my testing has gone, PDO requires that something is provided in the password field so no matter what config setup you're using it will try to use its own syntax. If there is no password, then the password field should be set to an empty string i.e. '' and it submits that empty string as your password. The short answer is, AFAIK it's not possible to do what you're trying.
If your primary concern is having a password revealed due to someone finding a way to snoop around your PHP files, having it placed in a client config is a good method to try to circumvent that but there are native PHP methods that can also hide the sensitive information from potential security breaches stemming from leaked PHP file contents.
I answered a similar question not too long ago that talks about the three main recommendations I'd make regarding PHP security for SQL passwords here: Set up PDO connection without password which mostly includes moving the php script outside your webdata directory as well as protecting php files from direct access even if accessed by using redirects to hide their existence and having code that returns a 404 error if the files themselves are directly accessed. Also I'd suggest making PHP-exclusive accounts that have very limited access scopes so in case someone does find a way to inject sql, they won't be able to do or see much. For examples of what I'm talking about, feel free to follow the link to read up.

Agile Toolkit 4 (ATK4) database connection failed

I'm trying to get the examples to work in Agile Toolkit, but I get the database connection failed error. I created a MySQL database, imported the schema.sql file, and updated the config.php file with the correct database name, database username, and password.
Here's the DSN line in config.php (fake username:password substituted)
$config['dsn']='mysql://admin123:pw12345#localhost/ATKexample';
It seems to be pointing to the right place, because the error changes if I put the wrong password into config.php. The first error message below is what I get with the correct password, and the second one below is what I get if I use an incorrect password.
PDO error: SQLSTATE[42000] [1044] Access denied for user
'admin123'#'localhost' to database 'ATKexample'
PDO error: SQLSTATE[28000] [1045] Access denied for user
'admin123'#'localhost' (using password: YES)
I can't figure out what I'm doing wrong. I don't know if it is a problem with the way the MySQL database is setup or if I need to change something in my ATK example files. Can anyone suggest a troubleshooting strategy?
Edit: I didn't have my user privileges setup right in MySQL. Problem solved.
Log into your mysql console and grant permissions for user admin123
grant all on `ATKexample`.* to 'admin123'#'localhost' identified by 'pw12345';

Why PHP MySql connection do not work?

I'm using MySQL given from A2Hosting. There I can create users and add them to databases. And i hosted my php codes inside web folder in symfony php structure. I dont want to use any symfony commands or functions. I have my own php calls to DB. But my Php codes gives the following error. But the user names and passwords are correct.
Query failed : Access denied for user 'games_user'#'localhost' to database 'games'
Granting privileges to user 'games_user' could not be enough. You must grant privileges to 'games_user'#'localhost', specifiying the host, even for localhost.
Granting all privileges for instance should look like this:
GRANT ALL PRIVILEGES ON database.* TO 'games_user'#'localhost' IDENTIFIED BY 'password';
Check permissions for user "games_user" in your "games" database. Access is denied to database "games"

Cannot connect to MySQL 4.1+ using old authentication

I'm trying to connect to a mySQL database at http://bluesql.net, but when I try to connect, it gives this error:
Connect Error (2000) mysqlnd cannot connect to MySQL 4.1+ using old authentication
I've looked into this, and it has to do with some old password scheme used before MySQL 4.1. Newer versions have the option to use old passwords, which I've read may cause this problem.
I'm running php 5.3, and connecting with mySQLi (new mysqli(...)). I'm hoping I can do something in the code to connect to the DB at bluesql.net - clearly I don't control how their database is set up. Downgrading php versions isn't an option.
Anyone have any ideas?
edit: This only applies if you are in control of the MySQL server... if you're not take a look at Mysql password hashing method old vs new
First check with the SQL query
SHOW VARIABLES LIKE 'old_passwords'
(in the MySQL command line client, HeidiSQL or whatever front end you like) whether the server is set to use the old password schema by default. If this returns old_passwords,Off you just happen to have old password entries in the user table. The MySQL server will use the old authentication routine for these accounts. You can simply set a new password for the account and the new routine will be used.
You can check which routine will be used by taking a look at the mysql.user table (with an account that has access to that table)
SELECT `User`, `Host`, Length(`Password`) FROM mysql.user
This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well).
Either use the user management tools of the MySQL front end (if there are any) or
SET PASSWORD FOR 'User'#'Host'=PASSWORD('yourpassword');
FLUSH Privileges;
(replace User and Host with the values you got from the previous query.) Then check the length of the password again. It should be 41 now and your client (e.g. mysqlnd) should be able to connect to the server.
see also the MySQL documentation:
* http://dev.mysql.com/doc/refman/5.0/en/old-client.html
* http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html
* http://dev.mysql.com/doc/refman/5.0/en/set-password.html
If you do not have control of the server
I just had this issue, and was able to work around it.
First, connect to the MySQL database with an older client that doesn't mind old_passwords. Connect using the user that your script will be using.
Run these queries:
SET SESSION old_passwords=FALSE;
SET PASSWORD = PASSWORD('[your password]');
In your PHP script, change your mysql_connect function to include the client flag 1:
define('CLIENT_LONG_PASSWORD', 1);
mysql_connect('[your server]', '[your username]', '[your password]', false, CLIENT_LONG_PASSWORD);
This allowed me to connect successfully.
Edit: as per Garland Pope's comment, it may not be necessary to set CLIENT_LONG_PASSWORD manually any more in your PHP code as of PHP 5.4!
Edit: courtesy of Antonio Bonifati, a PHP script to run the queries for you:
<?php const DB = [ 'host' => '...', # localhost may not work on some hosting
'user' => '...',
'pwd' => '...', ];
if (!mysql_connect(DB['host'], DB['user'], DB['pwd'])) {
die(mysql_error());
} if (!mysql_query($query = 'SET SESSION old_passwords=FALSE')) {
die($query);
} if (!mysql_query($query = "SET PASSWORD = PASSWORD('" . DB['pwd'] . "')")) {
die($query);
}
echo "Excellent, mysqli will now work";
?>
you can do these line on your mysql query browser or something
SET old_passwords = 0;
UPDATE mysql.user SET Password = PASSWORD('testpass') WHERE User = 'testuser' limit 1;
SELECT LENGTH(Password) FROM mysql.user WHERE User = 'testuser';
FLUSH PRIVILEGES;
note:your username and password
after that it should able to work. I just solved mine too
On OSX, I used MacPorts to address the same problem when connecting to my siteground database. Siteground appears to be using 5.0.77mm0.1-log, but creating a new user account didn't fix the problem. This is what did
sudo port install php5-mysql -mysqlnd +mysql5
This downgrades the mysql driver that php will use.
Had the same issue, but executing the queries alone will not help. To fix this I did the following,
Set old_passwords=0 in my.cnf file
Restart mysql
Login to mysql as root user
Execute FLUSH PRIVILEGES;
If you do not have Administrator access to the MySQL Server configuration (i.e. you are using a hosting service), then there are 2 options to get this to work:
1) Request that the old_passwords option be set to false on the MySQL server
2) Downgrade PHP to 5.2.2 until option 1 occurs.
From what I've been able to find, the issue seems to be with how the MySQL account passwords are stored and if the 'old_passwords' setting is set to true. This causes a compatibility issue between MySQL and newer versions of PHP (5.3+) where PHP attempts to connect using a 41-character hash but the MySQL server is still storing account passwords using a 16-character hash.
This incompatibility was brought about by the changing of the hashing method used in MySQL 4.1 which allows for both short and long hash lengths (Scenario 2 on this page from the MySQL site: http://dev.mysql.com/doc/refman/5.5/en/password-hashing.html) and the inclusion of the MySQL Native Driver in PHP 5.3 (backwards compatibility issue documented on bullet 7 of this page from the PHP documentation: http://www.php.net/manual/en/migration53.incompatible.php).
IF,
You are using a shared hosting, and don't have root access.
you are getting the said error while connecting to a remote database ie: not localhost.
and your using Xampp.
and the code is running fine on live server, but the issue is only on your development machine running xampp.
Then,
It is highly recommended that you install xampp 1.7.0 . Download Link
Note: This is not a solution to the above problem, but a FIX which would allow you to continue with your development.

Categories