Question

I want to write a cron job that passes a shell script to log into a MySQL database and perform some queries. To connect to the database as a certain user, I wanted to know what options I have to hide or not use the password?

The kind of answer I'm looking for is the Trust Authentication in PostgreSQL using a Trust User, so that no password is required.

I know you can create a password configuration file in the server but I want something more dynamic since passwords change often.

What are my options?

Was it helpful?

Solution

Simple task to do !! in this link you will find a how to article to show you how this is done.

I will describe it here ! -chose the user you what to login without a password and create a file in your home directory as the one bellow called .my.cnf and alter the permission on it as chmod 600. - it should do the work - i use this with my admin user.

[mysqldump]
user = --user used to run backup
password = --password of the user
[mysql]
user= --user used to run backup
password= --password of the user

Example of how this works:

[root@BIH001 ~]# cat /root/.my.cnf
 [mysqldump]
user = root
password = ***********
[mysql]
user= adrian
password= ***********

Next i switch to this user (adrian a SO level) :

[root@BIH001 ~]# su - adrian

And connect to mysql without password or any user :
(sure you will have to have this account present in the database as well as he will only see what ever you give him the rights to see)

[adrian@BIH001 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 962888
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Alter the .my.cnf file and add the root user to connect without password:

[root@BIH001 ~]# cat /root/.my.cnf
 [mysqldump]
user = root
password = ***********
[mysql]
user= root
password= ***********

[root@BIH001 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 963365
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

I hope this made it clear for you !!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top