Ubuntu – Connecting to MySQL in Bash (without MySQL installed)

bashMySQL

I am trying to connect to a remote MySQL database in Bash. On the server hosting the database I can type:

mysql -u _username_ -p

to connect.

I would like to be able to type:

mysql -h _host_ -u _username_ -p

to connect from another server. I don't have MySQL installed on the client so the command is not found. Is there something I can install (apt-get preferred) besides the entire MySQL-server so I can use the mysql commands in bash?

Best Answer

To install the command line MySQL client you should do:

sudo apt-get install mysql-client

and then you can do

mysql -h HOST -P PORT_NUMBER -u USERNAME -p

However you may need to alter the set up of the MySQL server. By default on ubuntu the MySQL server will only accept connections from the local server. The setting is called bind-address and is set in /etc/mysql/my.cnf. By default it is 127.0.0.1 - you should change it to the IP address of the server. If the server has multiple IP addresses you can choose just one IP address (say for the internal network) or have MySQL listen to all IP addresses by making the value 0.0.0.0

You will also need to ensure that the MySQL user can access the database. Following instructions on the internet, you may have created user 'myname'@'localhost' - that user will not be able to connect remotely. To create a new user who can connect from your client IP address you need to do something like:

GRANT ALL PRIVILEGES ON database_name TO 'username'@'192.168.0.51' IDENTIFIED BY 'password';

Read a little more about the address specification MySQL uses.

Finally don't forget to ensure that the server firewall will allow access to the MySQL port - the default is 3306.

Related Question