MySQL connection problems when supplying password - php

When I try to connect to the local MySQL database I am getting different results from these three methods:
It works when I connect with the following from the prompt:
mysql -u root -p
This way prompts me for the password, and once typed I am connected.
It works when I connect with PHP and supply the server(localhost), username(root), and password.
It does not work when I connect with PHP to run a command like this:
exec('mysql -P 3306 -u root -p'.$password.' databasename < '.$scriptfilename);
I also cannot get it to work from the prompt when I supply the password in the first line.
When I query the mysql.user table for the user root there are 3 entries:
root#::
root#127.0.0.1
root#localhost
There error I get when supplying the password in the same line is:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Anybody know whats going on?

If your password contains special characters that the shell will interpret, you'll have problems unless you escape them. For example lots of punctuation will cause issues, such as !, >, <, | and more.
Generally though, do not put the password on the command-line if you can possibly help it!
To quote MySQL's 5.3.2.2. End-User Guidelines for Password Security document:
Use a -pyour_pass or --password=your_pass option on the command line.
This is convenient but insecure, because your password becomes visible to system status programs such as ps that may be invoked by other users to display command lines.
The other option is to store the password in a file that only the relevent users can read, and use the client config file:
Store your password in an option file.
[client]
password=your_pass
Then the command-line would be:
mysql --defaults-file=/path/to/file_with_password

Just add the host parameter like this:
exec('mysql -P 3306 -h localhost -u root -p'.$password.' databasename < '.$scriptfilename);

You could try using --password= rather than -p, I have found it more reliable

Related

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: NO) cant find any solution [duplicate]

so i installed the MySQL application for the first time. firstly i saw the command line client is not opening so i searched for solutions. they said i must go to the bin directory and run it manually. and after i run the cmd mysql -uroot -p and run it and enter password, it gives me the error: ERROR 1045 (28000): Access denied for user 'root'#'localhost' i tried every single solution on stackoverflow including disabling permissions, running manually which i mentioned above, starting the service from service.msc, running it with password and without.... it just doesnt want to work.
appreciate any help in advance.
GENERIC MYSQL INFO
To start with, read the mysql manual: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
The steps will show you how to shut down the service and start it with an overriding command that doesn't require passwords, then you reset the password. From the manual:
Stop the MySQL server, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.
Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables:
shell> mysql
In the mysql client, tell the server to reload the grant tables so that account-management statements work:
mysql> FLUSH PRIVILEGES;
Then change the 'root'#'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different hostname part, modify the instructions to use that hostname.
MySQL 5.7.6 and later:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Or directly on the user table:
UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
XAMPP SPECIFIC
Stop the MySQL service. Open a command window. Change to the XAMPP MySQL directory:
> cd \xampp\mysql\bin\
Run the service without security (note you are running mysqld, not mysql):
> mysqld.exe --skip-grant-tables
The MySQL service will be running in this window, so open another command window and switch to the XAMPP MySQL directory:
> cd \xampp\mysql\bin\
Run the MySQL client:
> mysql
Update the password:
mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
Exit MySQL:
mysql> \q
Use task manager to cancel the mysqld.exe that is still running. Restart the mysql service.
I got the answer myself. Seemingly, if you get this error, it means that you need to reset your password. You can learn how to do that in MySQL from this link.
And don't forget to change the 5.7 version with your currently installed version in using commands (mine was 8.0).
After that, everything was working fine for me.

ClearDB: You need at least one of the super user privileges

I am attempting to import my ClearDB database in Heroku by running the command in the window:
mysql -u REDACTED -h us-cdbr-iron-east-02.cleardb.net -p REDACTED < C:/Users/KJA/Downloads/androidchatterdatabase.sql
After inputting the password, the error appears as:
ERROR 1227 (42000) at line 27: Access Denied; you need (at least one of) the SUPER privilege(s) for this operation
Then when seeing what the login is that would require the privileges it notes as:
Is there an alternative host address to use? How can I send my database schema to ClearDB in Heroku and run?
You need to select a database to work on by passing that as an argument to the command; you don't have privileges for any operation without a database (because that would let you control things at the "server level", not the database level).
Your command needs to be mysql -u YOURUSERNAME -h us-cdbr-iron-east-02.cleardb.net -p YOURPASSWORD heroku_XXXX < C:/Users/KJA/Downloads/androidchatterdatabase.sql (or whatever your database name is; I took "heroku_XXXX" from one of your other questions).

Cannot connect to MySQL server on Openshift

Recently, I've started using openshift & also deployed an application using PHP & MySQL. Yesterday, out of curiosity, I've removed the password for phpMyAdmin and guess what, now I'm unable to log in to both phpMyAdmin & mysql database.
I've tried both the passwords (the default one & the empty password) and uninstalled & re-installed the PHPmyAdmin catridge & also, force restarted the app several times but nothing worked. Now, I've no idea what happened. Any help is appreciated.
Hopefully this will help.
I assume what you did was go into phpmyadmin and click on 'users' then 'edit privileges' for one of the users, select 'no password' and hit save right? If so, then I think the following steps should help.
1.) ssh into your gear (you can use the rhc ssh command)
2.) run the mysql command
3.) You should get an error like this ERROR 1045 (28000): Access denied for user 'adminslULJTS'#'127.10.126.130' (using password: YES)
4.) Now, type in the command mysql -u $OPENSHIFT_MYSQL_DB_USERNAME -h $OPENSHIFT_MYSQL_DB_HOST -P $OPENSHIFT_MYSQL_DB_PORT -p
5.) When it asks for a password, just hit enter
6.) You should now be logged into the mysql shell
Now you need to reset your password to what openshift thinks it is.
1.) create another ssh session into your gear in another terminal, leaving the old one open
2.) run the command env | grep MYSQL
3.) this will give you the following information that you will need to reset your password:
OPENSHIFT_MYSQL_DB_HOST=127.10.126.130
OPENSHIFT_MYSQL_DB_PASSWORD=Il8-rVLIKSrx
OPENSHIFT_MYSQL_DB_USERNAME=adminslULJTS
Given the above information, go back to your ssh session that had the mysql connection open, and enter the following command:
set password for 'adminslULJTS'#'127.10.126.130' = PASSWORD('Il8-rVLIKSrx');
But you will need to replace the username, host, and password with the ones you got from the above step.
You should now be able to log into phpmyadmin with your old username & password that you can either view using the env | grep MSYQL command, or view in the web console for your application at openshift.com
It also might be worth reviewing this KB article: https://www.openshift.com/kb/kb-e1085-possible-complications-when-changing-your-database-credentials

php exec does not work as expected

My system: Ubuntu 11.10, LAMP Stack.
Issue:
I run the following in terminal and it does the back up correctly.
mysqldump -u root dbBugTracker > BAK/dbw.sql
But I include it in my php code like the following and it does NOT work.
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql');
Tips:
I tried putting a second parameter in exec but nothing is shown except the word Array. I print it out but nothing in it.
The file dbw.sql is actually created as a result of the exec function but it is 0 bytes.
I tried with the full path and without for mysql and the same result is seen. i.e., 0 bytes.
The folder BAK is within my project folder and I even gave it 777 permissions.
Even tried different file names and databases but the result is the same.
I appreciate any inputs on this. Thank!
MORE INFO:
I added 2>&1 to the exec line and NOW the file contains some text but NOT the DB dump. This is an error and I have no idea how to deal with this :(
Here's the error
mysqldump: Got error: 1045: Access denied for user 'root'#'localhost' (using password: NO) when trying to connect
So this is what the output file (dbw.sql) now contains.
Once again, it works fine when I run the dump from terminal.
You're running that dump command as a different user while on the command line. You are running it as Apache (I assume) when using exec(). Try adding a password parameter to the exec command, or creating an php-specific user in your db with appropriate privileges.
UPDATE:: As I guessed, you are not able to use the root user while executing this dump using PHP. So, create a new user.
First, login to your database from the command line. If you are the root user, don't bother with using -u root:
mysql
Now that you're logged in, go ahead and create a new user for Apache to use:
GRANT ALL ON database_name.* TO yourapacheuser#localhost IDENTIFIED BY 'yourpassword';
Go ahead and logout of mysql:
exit
Next, let's re-work your original code a bit...
$db_user = 'newusername';
$db_pass = 'pass';
$command = "mysqldump --add-drop-table -u $db_user -p$db_pass database_name > backup.file.sql";
$output = `$command`;
echo "Your database has now been backed up.";
Now, to execute the file, run this from the command line:
php path/to/sqldumpfile.php
Hopefully you can adapt this pseudo-code. Best of luck!
How do you print it?
Debug it like this:
<?php
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql', $output);
var_dump($output);
First, you should get it working on the command line. Verify that this produces the desired results prior to using PHP's exec():
/usr/bin/mysqldump -u root -p YOUR_PASSWORD dbTracker > BAK/dbT.sql
If it DOES work, then it's an issue somewhere in your PHP config.
The first thing to check is safe_mode. Are you using safe_mode? What version of PHP are you running?
Another possibility may be that your PHP user does not have permission to use the mysqldump binary.

SQLSTATE[28000][1045] Access denied for user 'root'#'localhost'(using password:NO)

When I try to connect to my backoffice in my website I get this error :
SQLSTATE[28000][1045]Access denied for user 'root'#'localhost'(using password:NO))
Can someone help me fix this?
You must use a password. Try to set one with "mysqladmin -u root password NEWPASSWORD"
You need to supply a password when logging into your database. When you connect to sql from php, make sure you're supplying this password.
If you set a root password previously, but have forgotten it, you can set a new password. I am working on a Windows machine, and was able to reset the password on the root of MySql using the directions http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html. I created a text file (mysql-init.txt) as follows:
UPDATE mysql.user SET Password=PASSWORD('[choose your password')
WHERE User='root';
FLUSH PRIVILEGES;
At the command line, I entered the following (either make sure you are in the mysql\bin directory or that your path environment variable includes a path to that directory), which will start the server:
mysqld --init-file=C:\\mysql-init.txt --console
This printed out a bunch of status statements. To stop the server, I typed this in another command line:
mysqladmin –u root –p[choose your password] shutdown
Of course, replace the [choose your password] with the same password you used in the mysql-init.txt file. In the first command line, you should get more status statements that indicate that the server is shutting down.
Now that you have a password for your root user, you can go back to the connection statement you are using to connect to the database and add the password and user.

Categories