爱生活、爱运动、爱分享、爱折腾、爱编程。

docker部署mysql并持久化数据到宿主机

1、拉取mysql镜像 (默认拉取最新的镜像)8.0.16
需要其它版本在后面加:tag(版本)

docker pull mysql

2、在宿主机创建持久化 mysql data 及mysql.cnf 

mkdir /usr/local/mysql/conf
mkdir /usr/local/mysql/data
vi /usr/loal/mysql/cnf/mysql.cnf

3、添加操作权限

chmod 777 /usr/local/mysql/data

4、运行并设置密码、端口映射、挂载本地盘进行持久化

 
docker run -itd -p 3306:3306 --name mysql -v /usr/local/mysql/conf:/etc/mysql -v /usr/local/mysql/data:/var/lib/mysql -v /usr/local/mysql/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=root mysql

5、设置远程访问

[root@uat ~]# docker exec -it mysql bash 
 root@123sre:/# mysql -uroot -p
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 10
 Server version: 8.0.16 MySQL Community Server - GPL
 Copyright (c) 2000, 2019, 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> grant all PRIVILEGES on . to root@'%' WITH GRANT OPTION;
 Query OK, 0 rows affected (0.01 sec)
 mysql> select user,host,authentication_string from mysql.user;
 +------------------+-----------+------------------------------------------------------------------------+
 | user             | host      | authentication_string                                                  |
 +------------------+-----------+------------------------------------------------------------------------+
 | root             | %         | $A$005$b4^gM@a
                                                )h
                                                  "2    lJiH3KjbwJqE/.NWRI6fd3Xe.ZN21o97Lpgyji5OO2BA |
 | mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
 | mysql.session    | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
 | mysql.sys        | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
 | root             | localhost | $A$005$'Y
 #
  x`[!3vUTVw.ddFCZbv.6wgLx3HfxcuvCS4lzS4gwZ1oa/Auq6rFy0 |
 +------------------+-----------+------------------------------------------------------------------------+
 5 rows in set (0.01 sec)
 mysql>

一个简单的docker部署mysql并持久化数据就完成了
如果需要主从同步点击这里

赞(0)
(转载本站文章请注明作者和出处 ,请勿用于任何商业用途)Mydrn Blog » docker部署mysql并持久化数据到宿主机

评论 抢沙发

评论前必须登录!