使用docker容器運行MySQL資料庫並持久化資料檔案

來源:互聯網
上載者:User

標籤:docker   art   預設   排除   row   var   statement   show   重啟   

1、下載mysql鏡像

# docker pull mysql

2、啟動mysql容器

# docker run -itd -v /data:/var/lib/mysql -p 33060:3306 --name mysqldb mysql bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
25d047a99917c6420412f36e1d0e2e6af38fc86539b2184d1ea7e5a47b955011

3、這裡我們發現啟動mysql容器出現warning,排除警告方式如下:

# vi /etc/sysctl.conf

添加:net.ipv4.ip_forward = 1

生效配置:

# sysctl -p
vm.max_map_count = 262144
net.ipv4.ip_forward = 1

重啟網路服務:

# service network restart
Restarting network (via systemctl):                        [  OK  ]

再次啟動mysql容器,發現警告消失:

# docker run -itd -v /data:/var/lib/mysql -p 33060:3306 --name mysqldb mysql bash
bbde59a2921981218441851d7527d22be3a0e37d3164fd1e07c4653d5dccc94d

注意:從上面啟動mysql容器命令中使用了-v、-p參數,具體意義解釋如下:

-v:為啟動的容器掛在volume,mysql資料庫預設資料目錄是容器中的/var/lib/mysql目錄,該目錄是容器內建的一個volume,如果不為mysql容器單獨配置資料目錄volume,那麼我們會發現重啟mysql容器後mysql資料庫中資料都丟失了。為了持久化mysql資料,我們在啟動mysql容器時添加了-v /data:/var/lib/mysql參數,把主機的/data目錄掛載到容器的/var/lib/mysql下。

-p:指定mysql容器到原生連接埠映射,將容器3306連接埠映射到主機的33060連接埠,這樣設定後,我們就可以在區域網路訪問mysql資料庫了。

4、進入mysql容器,啟動mysql服務:

# docker exec -it mysqldb bash
[email protected]:/# service mysql status
[info] MySQL Community Server 5.7.21 is not running.
[email protected]:/# service mysql start 
No directory, logging in with HOME=/
..
[info] MySQL Community Server 5.7.21 is started.

5、登入mysql資料庫,進行基本配置:

[email protected]:/# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chavin             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘mysql‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

此時,可以在區域網路內訪問docker容器啟動的mysql資料庫了。

# mysql -uroot -pmysql -h192.168.177.129 -P33060

使用docker容器運行MySQL資料庫並持久化資料檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.