Skip to content

Configuring MySQL

Tatiana edited this page Dec 11, 2017 · 1 revision

27th of September, 2017

Today we needed to run a bash script that updated a MySQL database. Obviously we could not introduce the username and password into the source code (especially as it’s hosted in a git repo on github.) What to do?

Well, mysql has a configuration file my.cnf where you can configure a default login for mysql client of any sort, including command line (which is what we want.)

To create default login credentials for mysql you might want to do the following:-

1. First locate the mysql configuration file to edit

$ locate my.cnf
/etc/alternatives/my.cnf
/etc/mysql/my.cnf
/etc/mysql/my.cnf.fallback
/var/lib/dpkg/alternatives/my.cnf
$ ll /etc/alternatives/my.cnf
lrwxrwxrwx 1 root root 20 Sep 10 10:13 /etc/alternatives/my.cnf -> /etc/mysql/mysql.cnf
$ sudo vim /etc/mysql/mysql.cnf
  1. Now add the following for your default login:-
[client]
user=<your_database_user_name>
password=<your_secure_password>
  1. Save the file

  2. Subsequently, test that your default login works:-

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5483
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, 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> quit
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1348
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

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

OracleBye
$

We found the following helpful:- https://stackoverflow.com/questions/2482234/how-to-know-mysql-my-cnf-location

Clone this wiki locally