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
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
RuslanN
  • 111
  • 1

2 Answers2

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
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Bryan
  • 11
  • 1
  • Thanks for the answer. So this is the only way? And there is no common "linux way" to do such things? – RuslanN Oct 16 '15 at 13:13
  • 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 Nov 09 '15 at 00:21
  • 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 Nov 10 '15 at 02:53
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`