美文网首页
docker安装mysql

docker安装mysql

作者: 无我_无他_有你 | 来源:发表于2022-02-11 08:59 被阅读0次

    官方地址:https://hub.docker.com/_/mysql?tab=tags

    下载mysql

    docker pull mysql:5.7
    

    安装完成后,用 docker images命令可以看到多了一个mysql的镜像

    [root@flower-core /]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    ffbb094f4f9e: Pull complete
    df186527fc46: Pull complete
    fa362a6aa7bd: Pull complete
    5af7cb1a200e: Pull complete
    949da226cc6d: Pull complete
    bce007079ee9: Pull complete
    eab9f076e5a3: Pull complete
    c7b24c3f27af: Pull complete
    6fc26ff6705a: Pull complete
    bec5cdb5e7f7: Pull complete
    6c1cb25f7525: Pull complete
    Digest: sha256:d1cc87a3bd5dc07defc837bc9084f748a130606ff41923f46dec1986e0dc828d
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7
    [root@flower-core /]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
    mysql        5.7       738e7101490b   7 days ago   448MB
    

    启动mysql容器

    docker run -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
    
    --name 给当前容器命名
    前三个-v 是目录挂载 相当于在linux下创建了快捷方式可以看到mysql容器中的数据信息
    

    查看docker正在运行的容器

    可以看到mysql已经启动成功了

    [root@flower-core /]# docker  ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
    91b71dd3e55a   mysql:5.7   "docker-entrypoint.s…"   16 seconds ago   Up 15 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
    
    

    进入linux bash 控制台,相当于进入mysql容器里面

    docker exec -it mysql /bin/bash
    
    root@91b71dd3e55a:/# ls
    bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    
    

    退出容器

    exit;
    

    查看mysql安装路径

    root@91b71dd3e55a:/# whereis mysql
    mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/share/mysql
    
    

    修改mysql配置文件

    配置字符编码为utf-8

    [root@flower-core /]# cd mydata/mysql/
    [root@flower-core mysql]# ll
    total 12
    drwxr-xr-x 2 root             root 4096 Dec 10 15:03 conf
    drwxr-xr-x 5 systemd-coredump root 4096 Dec 10 15:04 data
    drwxr-xr-x 2 root             root 4096 Dec 10 15:03 log
    [root@flower-core mysql]# cd conf/
    [root@flower-core conf]# ll
    total 0
    [root@flower-core conf]# vi my.conf
    

    将下面内容粘贴到文件中,保存并退出

    [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
    

    重启mysql容器

    docker restart mysql
    

    查看我们保存的配置文件

    [root@flower-core conf]# docker restart mysql
    mysql
    [root@flower-core conf]# docker exec -it mysql /bin/bash
    root@91b71dd3e55a:/# cd /etc/mysql/
    root@91b71dd3e55a:/etc/mysql# ls
    my.conf
    root@91b71dd3e55a:/etc/mysql# cat my.conf 
    [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
    

    设置docker重启时,mysql容器也自动重启

    docker update mysql --restart=always
    

    相关文章

      网友评论

          本文标题:docker安装mysql

          本文链接:https://www.haomeiwen.com/subject/cfrakrtx.html