Using Docker to build a Mysql cluster

Source: Internet
Author: User
Tags docker run

Introduction to the Software environment

Operating system: Ubuntu Server 64bit 14.04.1
Docker version 1.6.2
Database: Mariadb 10.10 (MARIADB is the branch that MySQL's father created after MySQL was acquired by Oracle, with better performance than the MySQL Open source version)

First step to installing Docker

For Ubuntu, it is recommended to install the latest version of Docker directly on the Internet, Apt-get in the older version. First get the installation script:
Down the Although the name is index.html, but actually is the script file, so we

chmod +x index.html

sudo./index.html
After the installation is complete, you can add the current user to the Docker user group as prompted, so that you don't need sudo every time you execute docker.

sudo usermod-ag Docker < your user name >

For CENTOS6, the first thing to do is to install the software source of the enterprise's common package

Yum Install Epel-release

and then
Yum Install Docker-io

UninstallList The packages you have installed
$ yum list installed | grep dockeryum list installed | grep dockerdocker- Engine.x86_64 1.7.1-1.el7 @/docker-engine -1.7.1-< Span class= "Hljs-number" >1.el7.x86_64.rpm    
    • 1
    • 2
    • 3
To delete an installation package
-y remove docker-engine.x86_64
    • 1
Remove mirrors/containers, etc.
-rf /var/lib/docker

Second step to run the MARIADB container

The first thing to do is to pull the data mirror down

Docker Pull Mariadb:latest

Note that if you do not add: Latest tags, docker will pull all the mirrored versions down.

Then we can start mirroring, the parameters need to pay attention to a few points:

1,-name < give the container a friendly name >
2,-e Mysql_root_password = ' < give the database a ROOT user password > '

3,-p < map to native port >:3306

4,-v < Native database storage directory >:/var/lib/mysql

5, set Mysql_user, Mysql_password, mysql_database environment variables can make the container in

The runtime creates both the database you need and the user with full permissions and their corresponding password

6, setting the value of the term environment variable can solve the problem that the container cannot enter MySQL console. For images that are not created by themselves, the created container may not be able to meet the requirements at once, and it is recommended that you write the Run command as a script and use

Docker Inspect < container name >

Look closely at the container information and follow the ports and file directories exposed by the image. If the discovery does not reach the requirements, use
Docker rm-r < container name >
Modify the run script to run again after deleting the container until you are satisfied.
here is my Run command:
Docker run--name mdb1 \
- P 13306:3306 \
-v/home/wonders/docker_mdb1_data:/var/lib/mysql \
- e mysql_root_password=wondersgroup \
- e mysql_user=medical_waste \
- e mysql_password=medical_waste \
- e mysql_database=medical_waste \
- e term=linux \
- D mariadb

The third step is to configure one Master one from the cluster

3.1

Then start another container as the slave database, because the mirror does not support entering the MySQL console inside the container, so it still needs to expose the port for LAN access, but the primary database can easily be linked in as an accessible host master_db.

Docker run--name < from database name >-e mysql_root_password=< from database ROOT password >--link < from Database container name >:master_db-d mariadb

3.2

Next, you need to configure two databases, the premise is that the image does not have a VI, so in two

In a container:

Apt-get Update

Apt-get Install Vim

This allows us to modify the configuration file within the container.

Another way is that we operate in the primary database container, and then use the
Docker Commit < Primary database container name > MARIADB

This creates a container that contains VI.

3.3
For the convenience of storytelling, now assume: We have a database, MDB1 and MDB2, mdb1 us as the primary database, MDB2 as from the database.
First modify the primary database:
Docker exec-it Mdb1/bin/bash

After entering the main database container,

Remove the comments from the "Server-id = 1" line, save, exit the container, and

Docker Restart MDB1
Similarly, changing the MY.CNF from the database to "Server-id = 2" requires a number larger than the main Server-id, and the MDB2 also needs to be restarted.

3.4
Using the client connection on the main database, here I am using MySQL Workbench, from the database because of security considerations and no public port to the host, only to enter the container of the MySQL console to operate.

Execute the SQL script in the primary database:

/* Set the account number, password/* for synchronization

Grant Replication Slave on * * to ' sync ' @ ' percent ' identified by ' sync ';
/* Save permission Settings */
Flush privileges;
/* To view the primary data log status, you need to remember the query result File and the Position value, which is the start of the log copied from the database */
Show master status;

Execute the SQL script from the database:

/* If sync is turned on, stop syncing */

Stop slave;

/* Set the primary database */

Chanage Master to

Master_host= ' master_db ',

master_user= ' Sync ',

master_password= ' Sync ',

master_port=3306,

Master_log_file= ' < main database query for the resulting file value > ',

master_log_pos=< positions value of main database query >;

Here is an example of my script:

Change Master tomaster_host= ' master_db ', master_user= ' sync ', master_password= ' sync ', Master_port=3306,master_log_ File= ' mariadb-bin.000004 ', master_log_pos=789;/* open Copy from Database */

Start slave;

Finally, you can pass

show slave status;

View the synchronization situation.
At this point we have built a Docker-based MARIADB DB cluster.

Using Docker to build a Mysql cluster

Related Article

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.