docker安装MySQL

写在前面

安装了docker环境 再来看下如何使用docker安装MySQL。

1:下载镜像

[root@localhost ~]# docker pull mysql:8.0
Trying to pull repository docker.io/library/mysql ... 
8.0: Pulling from docker.io/library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
...
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for docker.io/mysql:8.0
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql     8.0                 3218b38490ce        20 months ago       516 MB

2:准备容器使用的本地目录

用于挂载容器数据和配置文件,日志等。

[root@localhost ~]# mkdir -p /usr/local/docker/mydata/mysql/conf
[root@localhost ~]# chmod -R 755  /usr/local/docker/mydata/mysql/

vim /usr/local/docker/mydata/mysql/conf/my.cnf
 
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
secure_file_priv=/var/lib/mysql

3:启动容器

[root@localhost ~]# sudo docker run -p 3316:3306 --name mysql \
> -v /usr/local/docker/mydata/mysql/log:/var/log/mysql \
> -v /usr/local/docker/mydata/mysql/data:/var/lib/mysql \
> -v /usr/local/docker/mydata/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:8.0
d939e6aed5778a04efdf613df3afe04e7b79aac47260292094fab603c6e375de

本地的3316端口,映射到容器的3306端口。启动成功后查看容器信息:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d939e6aed577        mysql:8.0           "docker-entrypoint..."   3 seconds ago       Up 2 seconds        33060/tcp, 0.0.0.0:3316->3306/tcp   mysql

4:配置容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d939e6aed577        mysql:8.0           "docker-entrypoint..."   3 seconds ago       Up 2 seconds        33060/tcp, 0.0.0.0:3316->3306/tcp   mysql
[root@localhost ~]# docker exec -it mysql ./bin/bash
root@d939e6aed577:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
...
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.27    |
+-----------+
1 row in set (0.00 sec)

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

mysql>  

5:外部连接MySQL服务器

C:\Users\Administrator>mysql -h 192.168.64.129 -P 3316 -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2016, 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> select version();
+-----------+
| version() |
+-----------+
| 8.0.27    |
+-----------+
1 row in set (0.00 sec)

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

写在后面

参考文章列表

Docker安装MySQL 8.0镜像,简易上手

Linux下安装docker环境

你可能感兴趣的:(杂,docker,mysql,adb)