Donnie

不积跬步无以至千里

docker上安装mysql8.0

安装并运行mysql步骤

  1. 先查询一下mysql
    docker search mysql

  2. pull mysql 版本是8.0
    docker pull mysql:8.0

  3. 创建挂载目录,持久化数据,日志,配置
    mkdir -p /data/mysql/data /data/mysql/logs /data/mysql/conf
    cd /data/mysql/conf
    vi my.conf

    
    
            [mysqld]    
    
    
        pid-file        = /var/run/mysqld/mysqld.pid
        socket          = /var/run/mysqld/mysqld.sock
        datadir         = /var/lib/mysql
        secure-file-priv= NULL
        # Disabling symbolic-links is recommended to prevent assorted security risks
        symbolic-links=0
    
    
        port = 3306
        character-set-server=utf8
        #忘记密码时使用
        #skip-grant-tables
        #设置协议认证方式 
        default_authentication_plugin=mysql_native_password
        [mysql]
        default-character-set=utf8
    
    
    
  4. 运行mysql

docker run -p 3306:3306 --name mysql -v $PWD/conf/my.conf:/etc/mysql/my.conf -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0

使用mysql

  1. 进入mysql容器
    docker exec -it mysql /bin/bash
    mysql -uroot -p

  2. 容器外进入mysql
    docker exec -it mysql mysql -uroot -p

  3. 停止mysql
    docker stop mysql

  4. 启动mysql
    docker start mysql

  5. 修改端口映射
    vim /var/lib/docker/containers/{容器id}/hostconfig.json
    修改PortBindings参数配置,宿主机13306端口映射容器3306端口示例:
    “PortBindings”:{“3306/tcp”:[{“HostIp”:””,”HostPort”:”13306″}]},”

参考资料

https://hub.docker.com/_/mysql
http://www.runoob.com/docker/docker-install-mysql.html
https://www.one234.com/share/973/update-docker-container-port-map/

赞赏支持