Deploy MySQL database with Docker

Source: Internet
Author: User
Tags docker ps docker run

In the previous two blogs, we discussed how to install and use Docker, and how to deploy an Apache server in Docker and access the server in an external computer

Let's discuss how to use Docker to deploy a MySQL database and access the database on an external computer:

Note: If you don't understand some of the following commands, check out my first blog post


The MySQL database is already included in the XAMPP installed above, so I will not reinstall MySQL.

First, follow the tutorial above to install the XAMPP in the native computer, and configure it. BASHRC;

Then start the newly created mirror UBUNTU2 and map the 8080 port of the native computer to port 3306 of the container (MySQL default listening 3306 port):

sudo docker run–i–t–p 8080:3306 Ubuntu2/bin/bash

After executing the above command, you can go to the command line of the container.

Next, enter the following command on the container command line to start MySQL:

/opt/lampp/lampp start

And then into MySQL.

Mysql–u Root

See what databases are in MySQL:

show databases;

Open another command line terminal on the local computer, start the local computer MySQL (this computer will also install XAMPP)

/opt/lampp/lampp start

Connect to the MySQL database in the above container:

MySQL-  h 0.0.0.0–p 8080-  u root
(You can see the port mapping of the container with sudo docker PS)


The parameter H represents the MSYQL (host) on which machine to enter, and P (uppercase) indicates the port number.

No accident, you can now connect to the container in MySQL;

See which databases are available:

show databases;

Compare the results of this return to the same results as just now.

Then create a new database:

Create DATABASE HelloWorld;

Go back to the container command line and see what databases you have:

show databases;

At this point, you can see that the newly created database in the native computer is HelloWorld.

The rest of the operation of the database is the same.

Here we go. How to deploy a MySQL database in Docker and access the database on an external computer.



Problems:

1. Lost connection to MySQL server error occurred:

Workaround:

First go to the directory (if your MySQL is installed via XAMPP)/opt/lampp/etc:


Cd/opt/lampp/etc

Then use VIM to open my.cnf and find this line:

Bind-address = 127.0.0.1

Comment It out and ignore it if there is no line.

then find the parameters in the [mysqld] section, create a new line after the configuration, and add the following parameter:

Skip-name-resolve
Save and exit.

Then restart XAMPP:

/OPT/LAMPP/LAMPP restart

It should be solved by here.

If your MySQL is not installed through XAMPP, then my.cnf generally will only be stored in/etc/my.cnf or/etc/mysql/my.cnf;

Change the my.cnf to the same as above, then restart MySQL:

/etc/init.d/mysql restart

2. Host IP is not allowed to connet error such as MySQL server

Workaround:

Go to MySQL:

Mysql-u Root

Then authorize the other user:


GRANT all privileges on * * to ' root ' @ '% ' with GRANT OPTION; FLUSH privileges;

It should be solved by here.

If it is resolved, please refer here: http://joinyo.iteye.com/blog/1489380




Deploy MySQL database with Docker

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.