Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto authenticate password in mysql

I am new to MySql. In postgres, we can use .pgpass and save user password so that the database can automatically authenticate your password whenever you access or execute your sql script. I don't have to enter password.

So is there any way to do the same thing for mysql on linux?

Thanks

like image 351
novo Avatar asked Oct 03 '13 23:10

novo


2 Answers

Yes, you can store default credentials and other options in your home directory, in a file called $HOME/.my.cnf

$ cat > $HOME/.my.cnf
[client]
user = scott
password = tiger
host = mydbserver
^D

In MySQL 5.6, you can also store an encrypted version of this file in $HOME/.mylogin.cnf, see http://dev.mysql.com/doc/refman/5.6/en/mysql-config-editor.html

$ mysql_config_editor set --user=scott --host=mydbserver --password
Enter password: ********
WARNING : 'client' path already exists and will be overwritten. 
 Continue? (Press y|Y for Yes, any other key for No) : y

$ mysql_config_editor print --all
[client]
user = scott
password = *****
host = mydbserver
like image 140
Bill Karwin Avatar answered Oct 13 '22 02:10

Bill Karwin


You could use the command-line parameters available to the MySQL executable within a quick Bash script to accomplish this. See http://dev.mysql.com/doc/refman/5.6/en/mysql.html for the details. Basically, the following line would log you into MySQL

$>mysql --user=root --password=toor my_database

The command above would log you into the mysql database "my_database" as root using the password "toor"

Now but this into a quick Bash script (run_mysql.sh):

#!/bin/bash
/usr/bin/mysql --user=root --password=toor my_database

Make sure the script is executable:

chmod +x ./run_mysql.sh

Of course make sure this script is safely stored somewhere other users cannot access it such as your home folder and set the permissions accordingly.

like image 39
InfectedPacket Avatar answered Oct 13 '22 01:10

InfectedPacket