安装docker,直通
一.安装完docker后,执行以下命令拉去MySQL指定版本的镜像,我这里安装5.6.43
官方镜像地址:https://hub.docker.com/_/mysql?tab=tags
docker pull mysql:5.6.43
二.拉取完成之后,查看镜像
docker images
四.创建并启动MySQL容器
docker run --name rossc-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.6.43
–name:给新创建的容器命名,此处命名为rossc-mysql
-e:配置信息,此处配置mysql的root用户的登陆密码
-p:端口映射,此处映射主机3306端口到容器rossc-mysql的3306端口
-d:成功启动容器后输出容器的完整ID,如上图 31fab1e6ab79c7378c79c8e28e53f21910590ea7d9cf3a7cf265a007f1817f0d
最后一个mysql指的是mysql镜像名字+版本号
五.查看容器运行情况
docker ps
六.测试连接MySQL
遇到个问题1:
连接mysql成功后,项目启动后发现,有mysql大小写敏感的问题发生了。
执行查看大小写命令后发现, lower_case_table_names 的值是0 (0大小写敏感,1不敏感)
SHOW GLOBAL VARIABLES LIKE '%lower_case%';
处理方式如下:
1.进入mysql容器内部 ( 5dbd615fa7ca 是通过docker ps 查询出的CONTAINER ID的值)。
docker exec -it 5dbd615fa7ca /bin/bash
2.进入到/etc/mysql/mysql.conf.d目录:
3.修改mysqld.cnf文件,在其中加入如下一行代码,保存,exit退出
lower_case_table_names=1
4.重启mysql容器
docker restart 5dbd615fa7ca
5.问题解决。
整个截图如下:
【注意】:如果容器内,没有vim编辑器,执行apt-get install vim 即可,如果说有问题,可以先执行apt-get update后,再次执行apt-get install vim命令。一般是可以安装成功的。这样就可以vim 编辑mysql的配置文件了。
【我这里容器id不一样别太在意,你们安们的走就可以了。我这边文章是2次写的,故mysql容器id有差异,这并不影响参考。】
【补充1:】
如果安装mysql8+版本的话,用sqlyog连不上(报2058的错误:Plugin caching_sha2_password could not be loaded:乱码内容),用navicat是可以连上的,这个是由于sqlyog不支持最新mysql的加密方式导致的。需要执行以下命令:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你root账户的密码';
遇到个问题2:2024-05-20安装8.4.0版本的时候,报错如下
容器启动报错: Fatal error: Can’t open and lock privilege tables: Table ‘mysql.user’ doesn’t exist.
2024-05-20T04:42:12.662146Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-001146 - Table 'mysql.component' doesn't exist
2024-05-20T04:42:12.662177Z 0 [Warning] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-003543 - The mysql.component table is missing or has an incorrect definition.
2024-05-20T04:42:12.663175Z 0 [ERROR] [MY-010326] [Server] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2024-05-20T04:42:12.663250Z 0 [ERROR] [MY-010952] [Server] The privilege system failed to initialize correctly. For complete instructions on how to upgrade MySQL to a new version please see the 'Upgrading MySQL' section from the MySQL manual.
2024-05-20T04:42:12.663348Z 0 [ERROR] [MY-010119] [Server] Aborting
2024-05-20T04:42:14.193101Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.4.0) MySQL Community Server - GPL.
2024-05-20T04:42:14.193131Z 0 [System] [MY-015016] [Server] MySQL Server - end.
解决办法:
- 停止并删除容器
docker stop 容器id
docker rm 容器id - 清空映射的data目录
我这里映射的是:/data/mysql/data - 以如下命令重新启动容器
在启动容器命令的结尾加上--lower_case_table_names=1
docker run --name mysql --restart=always -p 3306:3306 -v /data/mysql/conf.d:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=你的密码 -d mysql --lower_case_table_names=1
- 查看日志
发现启动成功了。
docker logs -f --tail=10 容器id
[root@VM-16-16-centos data]# docker logs -f --tail=10 89ed67f05a07
2024-05-20T04:46:51.437577Z 0 [System] [MY-015015] [Server] MySQL Server - start.
2024-05-20T04:46:51.721622Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2024-05-20T04:46:51.723513Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.4.0) starting as process 1
2024-05-20T04:46:51.730174Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-05-20T04:46:52.179164Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-05-20T04:46:52.446848Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-05-20T04:46:52.446891Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-05-20T04:46:52.451463Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2024-05-20T04:46:52.484121Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.4.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2024-05-20T04:46:52.740035Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
END