docker安装mysql 8

Posted brucelong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker安装mysql 8相关的知识,希望对你有一定的参考价值。

1. 运行容器,自动下载镜像

docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql

2. 创建主机挂载配置文件

mkdir -p ~/i/apps/mysql/{conf,data}

 

  • conf存放配置文件
  • data存放数据库文件

3. 初始化配置文件

docker cp mysql:/var/lib/mysql ~/i/apps/mysql/data

 

  • 将数据库初始文件复制到/usr/mysql/data里面
docker cp mysql:/etc/mysql/my.cnf ~/i/apps/mysql/conf/my.cnf

 

4. 停止删除容器

docker rm -f mysql

 

5. 生成启动文件 - start.sh

[root@wanfei ~]# cd ~/i/apps/mysql/
$ cat <<EOF > start.sh
#!/bin/bash
HOST_NAME=mysql.wanfei.xyz
REDIS_DIR=`pwd`
docker stop mysql
docker rm mysql
docker run -d     --hostname ${HOST_NAME} \
    -p 3306:3306     --name mysql \
    --privileged=true \
    -v ${REDIS_DIR}/conf/my.cnf:/etc/mysql/my.cnf     -v ${REDIS_DIR}/data/mysql:/var/lib/mysql     -e MYSQL_ROOT_PASSWORD=123456     mysql
EOF

 

参数说明

  • --hostname ${HOST_NAME}: 设置访问的域名地址,${HOST_NAME}是上面定义的mysql.wanfei.xyz这个地址
  • -p 3306:3306: 把容器内的3306端口映射到宿主机3306端口
  • --privileged=true: 给容器加上特定权限 。否则会chown: changing ownership of ‘/var/lib/mysql/....‘: Permission denied
  • -v ${REDIS_DIR}/conf/my.cnf:/etc/mysql/my.cnf:把宿主机配置好的my.cnf放到容器内的这个位置中
  • -v ${REDIS_DIR}/data/mysql:/var/lib/mysql:把mysql持久化的数据在宿主机内显示,做数据备份
  • -e MYSQL_ROOT_PASSWORD=123456: 指定数据库初始访问密码,第一次设置才有小,后面重启以修改的密码为准
  • mysqlmysql镜像名,后面没有tag后,下载最新的tag,latest

6. 运行start.sh 启动mysql

[root@wanfei mysql]# sh start.sh 
mysql
mysql
f3d297633f4432495dc587d2860c212a371166897b6a380aceb0ebcdccc5dad8

 

7. 进入容器修改密码,开启远程连接

#进入容器
docker exec -it mysql /bin/sh
#进入mysql(有可能上面密码设置无效)
mysql -u root -p
#输入123456,如果失败,直接enter
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
#上面的root用户如果是localhost,只能localhost连接,不能远程连接,下面设置密码报错
mysql> ALTER USER root@% IDENTIFIED WITH mysql_native_password BY xxxxxx;
ERROR 1396 (HY000): Operation ALTER USER failed for root@%
#修改localhost为%
mysql> update user set host=% where user=root;
Query OK, 1 row affected (0.14 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER root@% IDENTIFIED WITH mysql_native_password BY xxxxxx;
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


 

 

以上是关于docker安装mysql 8的主要内容,如果未能解决你的问题,请参考以下文章

Docker篇章8:安装mysql/redis

Docker 安装 MySQL-8.0.23详解

docker安装mysql5.7mysql8.0

Centos 7.8下使用 Docker 安装 MySQL 8

Docker安装MySQL 8.0并挂载数据及配置文件

docker安装mysql 8