docker安装mysql 8

2018-09-29  本文已影响0人  wnfff

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

参数说明

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)
上一篇 下一篇

猜你喜欢

热点阅读