Accessing Mysql database installed in an LXD container

This is my second post on LXD containers. Sometimes, using a container is a cleaner way to install and use applications for a number of reasons:

  • You can install packages from any version of any distribution. That means, even if you are using an LTS release of Ubuntu like me, you don’t necessarily have to stick to an older version of a package. Or if some package is only supported on rpm based distributions, you don’t need to worry about it.
  • You can go ahead and completely uninstall/reinstall it at any time without worrying about residual files or configurations.
  • You can roll back to a previous state in no time if something goes wrong unexpectedly (lxd snapshots!)
  • And then there is the security perspective also (lxd containers are secure by nature).

Last weekend I tried running mysql database from a local container and connect to it from my local system. Here’s how:

  1. Create a brand new lxd container:
    lxc launch ubuntu:xenial mysql-test
  2. Create a bash session inside the container:
    lxc exec mysql-test bash
  3. Install mysql database server within the container:
    sudo apt install mysql-server
  4. Access the mysql database from inside the container as the root user:
    mysql -u root -p
  5. Allow remote access to root from any host:
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password'

    where password is the password to be used while accessing mysql from outside of the container.

  6. Enable built-in firewall configurer and allow mysql access from outside:
    sudo ufw enable && sudo ufw allow mysql
  7. Exit the mysql client. Open the file ‘/etc/mysql/mysql.conf.d/mysqld.cnf’ in your favourite text editor and comment the following line out by typing # at the starting of the line:
    #bind-address = 127.0.0.1
  8. Restart the mysql service:
    sudo service mysql restart
  9. Now exit the container. We need to check the IP of the container to access the database from the host. ‘lxc list’ should do the trick:
  10. Now from your host machine, try accessing the mysql server with the root password that you set in step #5:
    mysql -h 10.182.248.44 -u root -p

You should be all set now. But the IP address of the container box will change every time you start your container. There’s a way to stop it from changing. In my next post, I will share how to bind a static IP address to an lxd container.

Advertisements

I am a software engineer, an open-source enthusiast and a hobbyist blogger.

Tagged with: , , , ,
Posted in LXD, Shell, Ubuntu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: