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 father of MySQL after MySQL was acquired by Oracle
Created branch, better performance than 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:
wget https://get.docker.com
Down the Although the name is index.html, but actually is the script file, so we
chmod +x index.html
So that we can execute this file:
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 have to
sudo is required to 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
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 a mirror that is not created by itself, the container may not be able to meet the requirements at once, and it is recommended that the run
The command is written as a script and used after creation
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, make
Use
Docker rm-f < 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 13,306:3,306 \
-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
Next start another container as from the database, because mirroring does not support entering MySQL control inside the container
Ports are still exposed for LAN access, but the primary database can be easily linked into
As an accessible host master_db.
Docker run--name < from database name >-e mysql_root_password=< from database ROOT
Password >--link < primary 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 main
Database, mdb2 as from the database.
First modify the primary database:
Docker exec-it Mdb1/bin/bash
After entering the main database container,
Vi/etc/mysql/my.cnf
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" needs to be more than the main Server-id
Large numbers, MDB2 also need to be restarted.
3.4
Using a client connection on the primary database, here I am using MySQL Workbench, from the database because
Security considerations do not expose the port to the host, only to the container's MySQL console for operation.
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 Position value, is from the database
Log start of the system */
Show master status;
Execute the SQL script from the database:
/* If sync is turned on, stop syncing */
Stop slave;
/* Set the primary database */
Change 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 to
Master_host= ' master_db ',
master_user= ' Sync ',
master_password= ' Sync ',
master_port=3306,
Master_log_file= ' mariadb-bin.000004 ',
master_log_pos=789;
/* Turn on Copy from database */
Start slave;
Finally, you can pass
show slave status;
View the synchronization situation.
So we've built a Docker-based MARIADB database.

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.