Use command in file to enter mysql or other shell like environment

1

Let’s say we have password stored in /root/mysql-password. Is there a way to connect to MySQL without manually coping password from that file and entering to mysql -u root -p{passowrd}?

For example something like this:

mysql -u root -p | {command} /root/mysql-password

RuslanN

Posted 2015-10-15T22:33:08.727

Reputation: 111

Answers

1

The easiest way would be create /root/.my.cnf file (don’t forget to chmod 0600) with this format:

[client]
user=root
password=rootpass

If you’re doing something and can’t have the file at /root/.my.cnf, you should be able to create it as filename.cnf and then invoke MySQL with --defaults-file like this:

mysql --defaults-file=/root/squirrellyfiles/filename.cnf

Bryan

Posted 2015-10-15T22:33:08.727

Reputation: 11

Thanks for the answer. So this is the only way? And there is no common "linux way" to do such things? – RuslanN – 2015-10-16T13:13:46.637

Sorry, I'm not sure I understand exactly what you are asking. In Linux, you generally either have credentials in a flat config file (e.g. .my.cnf), a hashed or encrypted file, environment variables, through ssh keys, or a password prompt. Passing the password through the command directly is very insecure. Are you doing something where the defaults-file or .my.cnf doesn't work for you? – Bryan – 2015-11-09T00:21:12.007

My password is in a file and whenever I wanted to connect to mysql I had to to "cat file/with/password" copy it and "mysql -u user -ppassword" I thought it is possible to automate manual coping part – RuslanN – 2015-11-10T02:53:20.553

0

This may not be the preferred way of solving the particular problem, but the so-called "Linux way" would be to use backticks:

mysql -u root -p `cat /root/mysql-password`

Andrew Palmer

Posted 2015-10-15T22:33:08.727

Reputation: 129