Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass password from file to mysql command?

Tags:

shell

unix

mysql

I have a shell script which calls the mysql command with one parameter from external file, it looks like this (also I saw this example in other resources):

mysql --user=root --password=`cat /root/.mysql`

Bit it not working:

Failed to connect to MySQL server: Access denied for user 'root'@'localhost' (using password: YES).

I tried different quotes without success. How to pass it?

UPDATE 1: Found that I can pass password without space symbol. The problem in this, my root pass contains spaces.

like image 260
Sogl Avatar asked Jan 21 '16 05:01

Sogl


People also ask

How can I connect MySQL database with password?

Enter mysql.exe -uroot -p , and MySQL will launch using the root user. MySQL will prompt you for your password. Enter the password from the user account you specified with the –u tag, and you'll connect to the MySQL server.

What is the password in MySQL command line?

The default user for MySQL is root and by default it has no password. If you set a password for MySQL and you can't recall it, you can always reset it and choose another one.

In which file MySQL password is stored?

The password hashes are stored in the user table of the mysql database. The table files themselves are typically stored in a tree structure under /var/lib/mysql , but that location can be modified by build options or run-time configuration.

How are passwords stored in MySQL?

MySQL passwords for users are stored within MySQL itself; they are stored in the mysql. user table. The passwords are hashed by default using the PASSWORD() function.


2 Answers

Store your password in a protected mysql cnf file:

install -m 700 -d /srv/secrets/
install -m 600 /dev/null /srv/secrets/[email protected]
editor /srv/secrets/[email protected]

Store the password in the client.password ini property

[client]
password="password"

Include this file as the first argument in your mysql command:

mysql \
    --defaults-extra-file=/srv/secrets/[email protected] \
    --user=root \
    --host=localhost \
    --no-auto-rehash
like image 68
ThorSummoner Avatar answered Oct 12 '22 21:10

ThorSummoner


Use mysql_config_editor which is installed with the mysql client

mysql_config_editor set --login-path=dev --user=dbuser --host=localhost -p

Enter the password and then you can log in like this

mysql --login-path=dev
like image 27
user3183111 Avatar answered Oct 12 '22 22:10

user3183111