欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker # Docker 安装mysql 5.7

程序员文章站 2024-03-21 18:28:22
...

Docker # Docker mysql 5.7使用
docker 镜像拉取

docker search mysql     #查询mysql版本
docker pull mysql:5.7   #拉取mysql5.7版本
docekr image ls         #查看镜像
docker tag [IMAGE-ID] mysql:5.7  #修改TAG 名字:标签名
docker image ls   
#指定密码 映射端口 创建并运行mysql容器

创建并启动容器

docker run -it --name mysql1 -e MYSQL_ROOT_PASSWORD=zxc --hostname mysql -p 3306:3306 --privileged=true -d mysql:5.7

进入mysql容器后想要编辑配置文件,发现没有vim命令
解决方法:

apt-get update
apt-get install -y vim

远程登录mysql容器

[aaa@qq.com ~]# mysql -h 10.11.67.218 -uroot -pzxc
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 

cp宿主机sql文件到mysql容器
进入mysql容器导入(在外面导入失败所以进jspgou库内source)

docker cp jspgou.sql mysql:/
msyql -uroot -pzxc
create database jspgou;
use jspgou;
source jspgou.sql;

Docker # Docker 安装mysql 5.7
Docker # Docker 安装mysql 5.7

导入若出现下列错误(已解决)


ERROR 1067 (42000) at line 97: Unknown error 1067
#因mysql版本不同导入失败

解决方法:

在mysql容器中添加

aaa@qq.com:/# vim /etc/mysql/mysql.conf.d/mysqld.cnf 
#添加下列两行

sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUB
explicit_defaults_for_timestamp=1

重启mysql容器

数据库建成了,导入也成功了,浏览器访问OK了
Docker # Docker 安装mysql 5.7
乱码又出现了。。。。。。。。。。。。。
continue。。。。

==================================================
解决乱码
先查看使用的是什么编码:

mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)


都是使用的 客户都是latin1
所以

SET character_set_client = utf8;

SET character_set_results = utf8;

SET character_set_connection = utf8;

再次查询

mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

Docker # Docker 安装mysql 5.7
Docker # Docker 安装mysql 5.7
Docker # Docker 安装mysql 5.7
创建mysql容器

docker run \
--name mysql-jspgou -it \
--privileged=true \
-p 3306:3306 \
--hostname jspguo \
-e MYSQL_DATABASE="jspgou" \
-e MYSQL_ROOT_PASSWORD="zxc" \
-e TZ=Asia/Shanghai \
-d mysql-jspgou:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_general_ci \
--sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="password" \
-e MYSQL_ROOT_PASSWORD="password" \
-e TZ=Asia/Chongqing \
-d mysql:5.7 \
--character-set-server=utf8 \
-collation-server=utf8_general_ci \
--sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
相关标签: docker centos