CentOS Container – Installing MariaDB Server in Docker

containersdockerlinuxmariadb

I am trying to run a mariadb server on a docker image(CentOS Linux release 7.3.1611 (Core)).

I install mariadb with command yum install mysql. after installing complete, i try to execute command mysql but i got this error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

the directory in /var/lib/mysql is empty

the my.conf file in /etc contains:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

with running command systemctl start mariadb I got this error:

Failed to get D-Bus connection: Operation not permitted

Did I make any mistake?

Best Answer

It sounds like you're new to Docker. You may want to spend some time reading through documentation and examples to get a better feel for how things work.

First, simply installing software doesn't necessarily make it run, and that is certainly the case with CentOS and other Red Hat variants. On a bare metal host, you would simply run systemctl start mariadb to start it, but this only works because on a regular system, the /sbin/init component of systemd is started as part of the boot process.

Inside a docker container, nothing is running by default. For example, if you were to run ps -fe on your host, you would probably see hundreds of running processes.

If you were to run:

docker run centos ps -fe

You would see:

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:12 ?        00:00:00 ps -fe

Because of this, you can't start services using the normal mechanism. You will instead need to (a) start the service manually and (b) ensure that you have started it such that it will stay in the foreground (because a Docker container exits when the foreground process exits or puts itself into the background).

If you take a look at the official mariadb image, you will see that it ultimately just calls mysqld to start the service.

It sounds like you want to start mariadb inside a container and then try interacting it from within the same container. While this isn't the normal way of working with docker, you could...

$ docker run -it centos bash
[root@c1d6adf5c8bb /]# yum -y install mariadb-server
[root@c1d6adf5c8bb /]# /usr/libexec/mariadb-prepare-db-dir mariadb.service
[root@c1d6adf5c8bb /]# /usr/bin/mysqld_safe --basedir=/usr & 
[root@c1d6adf5c8bb /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

More typically, you would use a Dockerfile to build a mariadb image (or just use the official one), start it, and then use mysql on your host or in another container to interact with that mariadb instance.