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
  • 58,727
RuslanN
  • 111

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
  • 58,727
Bryan
  • 11
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`