Mysql的Docker镜像创建笔记
Mysql介绍
安装、打包Mysql 8.0镜像
容器准备
-
拉取基础镜像
docker pull centos:7
-
运行容器
docker run -dit -p 10000:3306 --name mysql8.0 centos:7
-
进入容器
docker exec -it [容器编号] /bin/bash
环境准备
-
安装安装mysql时所需环境和工具
yum -y install wget yum -y install vim yum -y install libaio* yum -y install numactl
-
创建用户
## 添加用户组 groupadd mysql ## 添加用户 useradd -g mysql mysql ## 设置mysql用户的密码 passwd mysql
-
创建目录
cd / ## 安装包目录 mkdir installationPackage ## mysql安装目录 mkdir apps
安装mysql
-
下载安装包
下载地址:https://dev.mysql.com/downloads/mysql/cd /installationPackage wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
-
解压安装包
cd /installationPackage xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz cd /apps tar xvf /installationPackage/mysql-8.0.20-linux-glibc2.12-x86_64.tar
- 修改mysql目录名称
cd /apps mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0.20
-
创建数据目录
cd /apps/mysql-8.0.20 mkdir data chown -R mysql:mysql /apps/mysql-8.0.20/data
-
安装mysql
cd /apps/mysql-8.0.20/bin ./mysqld --initialize --user=mysql --basedir=/apps/mysql-8.0.20/ --datadir=/apps/mysql-8.0.20/data/
执行完成后,会将mysql的初始密码输出
-
配置mysql
cd /apps/mysql-8.0.20/support-files vim mysql.server
修改mysql.server文件中的路径
basedir=/apps/mysql-8.0.20 datadir=/apps/mysql-8.0.20/data
启动mysql
-
启动mysql
cd /apps/mysql-8.0.20/support-files ./mysql.server start
-
添加软链接
ln -s /apps/mysql-8.0.20/bin/mysql /usr/bin
-
登录mysql
使用安装时的密码登录MySQL mysql -u root -p
设置密码、允许远程登录
-
修改密码
## 运行命令重置密码 alter user 'root'@'localhost' identified by '123456'; ## 刷新权限 flush privileges;
-
允许远程登录
show databases; use mysql; CREATE USER 'root'@'%' IDENTIFIED BY '你的密码'; GRANT ALL ON *.* TO 'root'@'%'; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码'; FLUSH PRIVILEGES;
打包docker镜像
```
docker commit -a "myname" -m "mysql_8.0" -p [刚刚安装mysql的容器编号] mysql_8.0:0.1
```
安装、打包Mysql 5.7
容器准备
-
拉取基础镜像
docker pull centos:7
-
运行容器
docker run -dit -p 10001:3306 --name mysql5.7 centos:7
-
进入容器
docker exec -it [容器编号] /bin/bash
环境准备
-
安装安装mysql时所需环境和工具
yum -y install wget yum -y install vim yum -y install libaio* yum -y install numactl
-
创建用户
## 添加用户组 groupadd mysql ## 添加用户 useradd -g mysql mysql ## 设置mysql用户的密码 passwd mysql
-
创建目录
cd / ## 安装包目录 mkdir installationPackage ## mysql安装目录 mkdir apps
安装mysql
-
下载安装包
下载地址:https://dev.mysql.com/downloads/mysql/cd /installationPackage wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
-
解压安装包
cd /apps tar zxvf /installationPackage/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
-
修改mysql目录名称
cd /apps mv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql-5.7.30
-
创建数据目录
cd /apps/mysql-5.7.30 mkdir data chown -R mysql:mysql /apps/mysql-5.7.30/data
-
安装mysql
cd /apps/mysql-5.7.30/bin ./mysqld --initialize --user=mysql --basedir=/apps/mysql-5.7.30/ --datadir=/apps/mysql-5.7.30/data/
执行完成后,会将root的初始密码输出
-
配置mysql
cd /apps/mysql-5.7.30/support-files vim mysql.server
修改mysql.server文件中的路径
basedir=/apps/mysql-5.7.30 datadir=/apps/mysql-5.7.30/data
启动mysql
-
启动mysql
cd /apps/mysql-5.7.30/support-files ./mysql.server start
-
添加软链接
ln -s /apps/mysql-5.7.30/bin/mysql /usr/bin
-
登录mysql
使用安装时的密码登录MySQL mysql -u root -p
设置密码、允许远程登录
-
修改密码
## 运行命令重置密码 alter user 'root'@'localhost' identified by 'HUtianyao123456-'; ## 刷新权限 flush privileges;
-
允许远程登录
show databases; use mysql; CREATE USER 'root'@'%' IDENTIFIED BY '你的密码'; GRANT ALL ON *.* TO 'root'@'%'; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码'; FLUSH PRIVILEGES;
打包docker镜像
```
docker commit -a "myname" -m "mysql_5.7" -p [刚刚安装mysql的容器编号] mysql_5.7:0.1
```