How to set mysql password automatically in bash script? - mysql

I have a bash script that opens queries from MySQL.
For example:
$ cat script.sh
mysql -u joe -D database -p ........ < first_query.sql
mysql -u joe -D database -p ........ < second_query.sql
This 2 lines inside my script.sh file return the results of the queries, but: Everytime I call the ‘mysql’ command, if it has the «-p» option, it asks for password TWICE (because I call mysql twice). How can I avoid this behavior?
I have tried passing my password as parameter, for example:
$ sh script.sh ‘mypassword’
and storing it in a variable and putting it after the «-p» option.
But it still asks for the password twice.

You can place your password staight after the -p. If you password is "123asd", the command will be:
mysql -u user -p123asd
Make sure there are no spaces between -p and your password.

Probably the simplest way would be to edit your my.cnf (found in /etc/mysql/ by default) to include your password, like so:
[client]
user = joe
password = ......

Related

mysqldump prompting password even with credentials saved at /home/user/.my.cnf

My ultimate goal is to create a cron script to automatically dump a selected MySQL database (ngsRunStats_FK) once a day. Please note that the MySQL user ngs_run_stats has all privileges on MySQL.
I was expecting the answer from Franklin here would do the job, though when I run on the terminal
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats -p ngsRunStats_FK --lock-tables=false > test.sql
I am still required to prompt my password. If I enter the password correctly, the dump will work as expected. Though, as stated previously, this should be done automatically i.e. without prompting for the password.
I am assuming that if I am being asked to prompt my password when I call the command on the terminal my cron (not shown here) script will not work. Or is this assumption wrong?
My /home/user/.my.cnf looks like so:
[mysqldump]
user = ngs_run_stats
password = mypassword
and has permissions 600
Try this:
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats -pngsRunStats_FK --lock-tables=false > test.sql
there should be no space between -p flag and the input password.
However, note that this is an insecure way of connecting to your database as the password will be in plaintext within the command and can be visible through the logs.
The mistake is that I am passing the -p flag, which will prompt the password. I misread the information from this website. So I should omit the -p flag and the database is actually specified by the --databases flag
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats --databases ngsRunStats_FK --lock-tables=false > test4.sql

Run mysql commands in bash script without logging in or adding -u root to every command

I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit

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
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
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.

Setting mysql as shell

I have a user on my machine that is only supposed to run mysql. Is there any way that I can set the shell of that user to mysql and login using password and username?
I know how to change the shell to the mysql binary
usermod -s /usr/bin/mysql
That is working indeed, only I can't provide a username/password in the program. Usually user/pw are given as
mysql -u $USER -p
I can not provide parameters for a shell as in
usermod -s "/usr/bin/mysql -u $USER -p" # Does not work!
Also using a simple shell-script as shell does not work:
#!/bin/sh # mysqlShell
/usr/bin/mysql -u $USER -p
----
usermod -s mysqlShell # does not work
So how can I provide parameters to a program I use as a shell for a user?
Thanks to Tom Regner I could figure out a solution using .my.cnf containing
[client]
host=localhost
user=$user
password=$pass
disable-auto-rehash
where mysql is set to the shell. I still would like give the password manually but this is the best I found.
Setup a $HOME/.my.cnf file for the user
[client]
host=localhost
user=mysqluser
password=mysqlpass
then set a bash as login shell and put the following in $HOME/.bashrc
exec mysql --host=localhost dbname
that should do what you want, while the user in question just has to give one password (the system account password on login).
exec replaces the shell process with the mysql process.
If this does not work as expected, you may need to adjust $HOME/.bash_profile to source .bashrc:
[[ -f ~/.bashrc ]] && . ~/.bashrc
It might be enough to provide an appropriate .my.cnf and setting /usr/bin/mysql as shell, but this way you can pass arbitrary commandline options/flags to the mysql client.
You can do that by editing the user's account details in the /etc/passwd and change the default shell.
You need a login password (unless you set up ssh appropriately). Use the following command: sudo passwd username to change that login password.
You also need a mysql password. Use SET PASSWORD Mysql request.
If you want the user to be connected to some fixed database with some fixed password, code a small C wrapper (then, make the executable only executable by your Unix user) doing mysql_real_connect, or calling some exec function for mysql --user=username --password=password databasename but I don't recommend doiing the later (because ps aux will show the password, and that is a security risk).
Perhaps, since MySQL is free software, you could customize the source code of mysql for your particular needs.
Perhaps using a restricted shell and carefully configuring it is better.

bash script command to inject a schema.sql into mysql db

i found this code but do not quite understand what the command is doing.
sudo -u test-user mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
i know the last part is using lib.model.schema.sql to create the tables and fields
the first part i dont quite understand: sudo -u test-user mysql -U test_traffic traffic
i know the command sudo and mysql
please explain?
thanks
Let's look at it bit by bit. Firstly the format
sudo -u username command
is an instruction to run command (which might be simple or complex) as the user username. So in your example, you are running the mysql command as the user test-user. You should note that this includes all the parameters to the mysql command - that's the entire rest of the line.
The command
mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
appears corrupt (certainly running it on 5.0.51a fails). It would make sense if the -U was a -u which would indicate that that the command was to be executed for mysql user test_traffic. If it was a -u you would then have an instruction to import the sql file into the traffic database.
So the combined instruction says, import the lib.model.schema.sql file into the database test_traffic using the mysql user test_traffic and executing the entire command as if you were logged-in as the user test-user.
Try Below steps for mysql:
mysql > -h hostname -u username -p password
mysql > use databasename;
mysql > source path/to/scriptfile
If you want to inject theschema.sql file into your database, with a shell script, simply use :
mysql -h [host] -u [username] -p[password] -D [database] < your_file
If you want to dynamicly tell which file should be loaded, replace your_file by $1 and pass the name of the file as an argument to your script.
Take care also to the -p option. There is no space between the -p and your password.