Docker use MARIADB

Source: Internet
Author: User
Tags mysql client docker ps docker hub docker run aliyun

This article is mainly divided into two parts.
The first part describes why you should use Docker and what is the advantage of using MARIADB in Docker by describing the use scenario.
The next section details how to install Docker, use mariadb mirroring, and how to mount data volumes and migrate data. Why to use Docker

During the development of the program, especially related to the development of the server, often deployed servers and the development of the computer is not the same environment, and in reality, different developers of different development environment, even the same person, the Office of the Development machine, their own laptop, the home computer use of the system, the environment is not the same. This could lead to a problem with a written program running on the server, or an unfinished program at the office, returning home to write code, and finding unexpected Crash due to different system environments.
Therefore, we have reason to find a way to let us focus on the problem, and from the constantly configure, debug the system environment, such as the cause of the problem of the chores freed. Docker is a sharp weapon that can solve this demand. advantages of using MARIADB in Docker

In my work, the development server is in the company, the production server is in the cloud, they are all based on the Ubuntu Linux system, while my company's development machine is a WINDOW10 WorkStation, there is also a MAC system laptop for mobile office and home use.
In the development process, the server and the development machine are three different systems, but because the development server belongs to the intranet of the company, so if you want to continue to work at home, the connection on the development server is not very convenient. For all three systems, it takes a lot of time and effort to install MARIADB and other corresponding services, configure and maintain, and share data in the database.
If you use Docker, the situation will be much simpler. Docker provides Windows, Linux, MacOS three system support, then only need to install the Docker service in three systems, and then use MARIADB Images, the Mount Docker provided by Volume in the shared database, can be a large number of Reduce the time spent maintaining, configuring different systems, and service versions. Docker Installation

Docker installation can refer to Docker official website. Windows and MacOS systems all have an installation package, Ubuntu system can he download installation package installation, detailed reference to the official website introduction.
After the installation is completed, due to domestic network reasons, the connection Docker Hub speed is a bit slow, so you can replace the domestic mirror source.
Here I use the Docker Hub mirroring accelerator provided by Aliyun, which requires registering a Aliyun developer account first. Detailed reference to the following operational documentation: Aliyun Docker mirror site.
After the Docker installation is complete, you can also change the location of the Docker Images as needed, because the Docker Images occupies a larger capacity, so you can develop your own machine or the server's specific situation changes. mariadb Mirroring Use

Docker provides a number of mariadb mirrors, which can be queried by the following command

Docker Search MARIADB

In general, we use the image provided by the official, the following is to get the download image, the default to get the latest version

Docker Pull Mariadb

Next, we will start a mariadb container

Docker run--name mariadb_test-e mysql_root_password=my-secret=pw-d mariadb
> f5605d02f9f50d1bc813423454fb421ba17513c440487f26b41e26844d652136
Docker ps-a
> CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
  f5605d02f9f5        mariadb             "Docker-entrypoint ..."   20 Seconds ago up      seconds              3306/tcp            mariadb_test

We can see that a mariadb container with ID F5605d02f9f5 is already running.
Next, we'll go into the container and view the database

Docker exec-it f5605d02f9f5 bash
> root@f5605d02f9f5:/#
# has entered the container f5605d02f9f5
root@f5605d02f9f5:/# Mysql-u root-p
Enter password:my-secret-pw
> Welcome to the MARIADB Monitor.  Commands End With; or \g.
Your mariadb Connection ID is 9
Server version:10.2.8-mariadb-10.2.8+maria~jessie mariadb.org binary distribution< C8/>copyright (c), 2017, Oracle, mariadb Corporation Ab and others.

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

MARIADB [(None)]>
# as root into the MySQL client
mariadb [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MySQL              |
| performance_schema |
+--------------------+
3 rows in Set (0.00 sec)
# you can see database information in the database

The MARIADB is already available, but cannot be connected remotely, so we need to map the ports to allow our database to be accessed remotely.

Docker run-d-P--name mariadb_connect-e mysql_root_password=my-secret-pw mariadb
> bf2280bb46bc3d624e6c0596c1545c39d4da249590a739401ec64635129d297b
Docker ps-a
> CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
  bf2280bb46bc        mariadb             "Docker-entrypoint ..."   7 seconds ago up       5 seconds        0.0.0.0:32769->3306/tcp   mariadb_connect

We can see that by using the-p argument, Docker will automatically assign an unused port to us, which is 32769, and then we can test the connection with the Navicat tool.


We can see that the connection test succeeds by correctly filling in the host address, port number, username, and password in the Navicat and then clicking Test Conection.
Above we bind the parameter-P is let Docker randomly map an available port, if we want to customize the mapped port, you can use the-P hostport:containerport. mount a data volume for saving database data

In order to facilitate the migration of data in the database, we can mount the data volume to achieve.

# to mount a local/data/db/mariadb as an example
Docker run-d--name mariadb_volume-p-v/data/db/mariadb:/var/lib/mysql

In this way, the data in the database will be saved on the local file/data/db/mariadb that we are mounting. We can migrate or back up this folder to implement the database migration.
Generally an automatically generated empty database file, about 100 trillion, and this folder contains a lot of files, so if the transmission through SSH or FTP will take a long time, here we can reduce the size of the folder by compressing packaging.

# Ubuntu Compressed
cd/data/db
tar zcvf mariadb.tar.gz mariadb
# MAC Window is compressed directly in the graphical interface

Package compression, the capacity will be significantly reduced, in Ubuntu, compressed file size of about 2 trillion. Then you can transfer this compressed file to the target computer, extract to the appropriate path to use.

# Ubuntu Decompression
tar zxvf mariadb.tar.gz

It is worth noting that if we create new users in the database, we migrate the data volumes that store the database data to other places, and then mount the database again, and those database users are also available. Therefore, you may not need to mysql_root_password this parameter if you are mounting a data volume with data. reference materials

docker--from getting started to practice
Library/mariadb-docker Hub

This article is the original content, by the same author earlier published in the Jane Book, Docker use MARIADB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.