docker 安装mysql8.0.26
一、环境
Ubuntu 20.04.4 LTS \n \l
docker 20.10.14
mysql 8.0.26
二、拉取镜像
sudo docker pull mysql:8.0.26
创建数据目录和配置文件
在宿主机创建放置mysql的配置文件的目录和数据目录,并且进行授权
sudo mkdir -p /data/mysql8.0/conf /data/mysql8.0/data /data/mysql8.0/logs
sudo chmod -R 755 /data/mysql8.0/conf /data/mysql8.0/data /data/mysql8.0/logs
三、创建配置文件
vim /data/mysql8.0/conf/my.cnf
添加以下内容到上述创建的配置文件中
[client]
#socket = /data/mysql8.0/mysqld.sock
default-character-set = utf8mb4
[mysqld]
datadir=/data/mysql8.0/data
socket=/var/lib/mysql/mysql.sock
log-error=/data/mysql8.0/logs/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
max_allowed_packet = 1G
innodb_log_file_size = 30M
innodb_log_buffer_size = 256M
innodb_file_per_table = 1
innodb_buffer_pool_size=1G
#innodb_file_format=barracuda
character_set_server = utf8mb4
collation_server = utf8mb4_bin
max_connections=200
lower-case-table-names=1
innodb_strict_mode = 0
secure_file_priv =
transaction-isolation = READ-COMMITTED
default_authentication_plugin=mysql_native_password
default-time-zone = '+08:00'
#skip-grant-tables
[mysql]
prompt="\\u@\\h : \\d \\r:\\m:\\s>"
default-character-set=utf8mb4
四、启动创建容器
root@dbabackup:/data/mysql8.0/conf# sudo docker run -itd --name=mysql8.0 -v /data/mysql/conf/my.cnf:/data/mysql8.0/conf/my.cnf -v /data/mysql8.0/data:/data/mysql8.0/data -v /data/mysql8.0/logs:/data/mysql8.0/logs -p 3306:3306 -e MYSQL_ROOT_PASSWORD=andyxi@mkl 9da615fced53
参数解释: -v : 挂载宿主机目录和 docker容器中的目录,前面是宿主机目录,后面是容器内部目录 -d : 后台运行容器 -p 映射容器端口号和宿主机端口号 -e 环境参数,MYSQL_ROOT_PASSWORD设置root用户的密码
五、查询容器
sudo docker ps -a
六、配置远程的Navicat连接
进入容器内部
root@dbabackup:/data/mysql8.0/conf# sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40f724a0475c 9da615fced53 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql8.0
32aa3bfd2d03 tapdata-docker.pkg.coding.net/dfs/flow-engine/dfs-flow-engine:v2.1.4-03 "docker-entrypoint.s…" 6 weeks ago Up 6 weeks amazing_chebyshev
root@dbabackup:/data/mysql8.0/conf# sudo docker exec -it mysql8.0 /bin/bash
七、连接mysql
mysql -uroot -pandyxi@mkl
使用mysql库
use mysql;
修改访问主机以及密码等,设置为所有主机可访问
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'andyxi@mkl';
刷新
flush privileges;
使用Navicat连接数据库时就可以正常连接了。