DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Using Docker to Create a MySQL Server

Using Docker to Create a MySQL Server

In this post, we take a look at how you can leverage Docker to create a MySQL server to help you with your development process. Read on for the details!

Rob Allen user avatar by
Rob Allen
·
Nov. 03, 17 · Tutorial
Like (6)
Save
Tweet
Share
16.06K Views

Join the DZone community and get the full member experience.

Join For Free

When working on test code on my computer, I usually use the built-in PHP server (php -S), which works nicely. Every so often, I need access to MySQL and I use Docker to temporarily create a MySQL server for me. This is how I do it.

The magic command is:

$ docker run --name mysql \
  -e MYSQL_USER=rob -e MYSQL_PASSWORD=123456 -e MYSQL_DATABASE=bookshelf \
  -p 3306:3306 -d mysql/mysql-server:5.7

This creates a Docker container called mysql on port 3306. We pass three environment variables: MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE, which are our credentials and the database name.

Client Access to the Database

If you have the MySQL command line client installed, you can access your database like this:

$ mysql --protocol=TCP -u rob -p123456 

We need to use TCP protocol as there's no socket in play here. If, like me, you're too lazy to type --protocol=TCP each time, then set it in your ~/.my.cnf file like this:

~/.my.cnf:

[client]
protocol=TCP

One of the easier ways to get a MySQL command line client on a Mac is to install MySQL WorkBench and add /Applications/MySQLWorkbench.app/Contents/MacOS to the path, as Homebrew seems to want to install the server, too.

Controlling the Container

Stop the container using docker stop mysql and restart it again with docker start mysql. When you're finished with it, you can delete it with docker rm mysql.

Restoring a Dump File

As these MySQL instances are temporary for me, I install a database schema using:

cat seed-mysql.sql | mysql --protocol=TCP -u rob -p123456 rob bookshelf

(Obviously, you'd use same credentials as you ran your container with!)

Fin

That's all there is to having a temporary MySQL install running locally for development.

MySQL Workbench Docker (software)

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Load Balancing Pattern
  • Automated Performance Testing With ArgoCD and Iter8
  • RabbitMQ vs. Memphis.dev

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: