0

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?

3
  • hide how? so it doesn't show up on the command line? Commented Mar 7, 2014 at 16:19
  • I don't want to supply the password directly at all, if that can be done. And hide in the sense that if someone can open and read my shell script, I do not want them to be able to see the password as normal text. Commented Mar 7, 2014 at 16:23
  • well, either you configure the account to have no password at all, or you have to store the password SOMEWHERE. Commented Mar 7, 2014 at 16:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

There's already a 'my.cnf' file there that doesn't have a username or password in it. Is this the same as '.my.cnf'?
Is a different one ! normally i have two - one /etc/my.cnf and one /root/.my.cnf (this one will grant access with no pass) - as the /etc/my.cnf will be written by the user who runs the mysql service and this might go worng to place a text pass in it !
I am still getting ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) even after creating the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.