安装docker
yum install -y docker.io
yum install -y epel-release
安装完成后修改docker镜像
我这边是centos7.5,所以修改镜像配置在/etc/docker/daemon.json
增加
{
"registry-mirrors": ["https://id.mirror.aliyuncs.com"]
}
其他系统自己去搜
安装mysql
搜索mysql镜像,或者直接安装最新版docker search mysql
拉取镜像docker pull mysql:latest
安装的是最新的mysql:8.0.19
查看可用镜像docker images
运行mysql镜像
docker run -d --name mysql -p 3306:3306 -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=你的密码 docker.io/mysql:8.0.19
关于参数的解释-d 后台运行
--name 设置别名
-p 端口映射 冒号前为本机端口
-v 目录挂载 冒号前为本机目录
-e 环境配置参数 这里设置的是root密码
最后其实我不太明白为何我的镜像运行要加docker.io/,网上搜到的镜像名都是直接mysql而已
mysql配置
运行脚本
首先要查看容器全IDdocker inspect mysql(这里是你的容器别名)
把之前服务器导出的sql脚本复制到docker容器中docker cp /usr/local/scripts/mysql-bak.sql 容器全ID:/usr/local/scripts
导入了之前服务器的脚本后,发现乱码,一查数据库编码show variables like 'character%';
长这逼样
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
什么年代了,为什么还不默认utf8,不太明白
故要设置my.cnf,又发现容器中没有vim,所以接下来继续在容器中安装vim
安装vim(其实是我当时脑瘫了,直接在主机上编辑好传到容器中就行了)
更新aptapt-get update
发现很慢,又去更新apt的镜像,地址在/etc/apt/sources.list
更新为用网易的镜像
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
更新后速度飞快,不多逼逼,继续安装apt-get update
apt-get install vim
结果报错
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
vim : Depends: libtinfo5 but it is not going to be installed
幸好还有小学英语水平,这最后一句说的是没有libtinfo5的依赖,那我再安装这个玩意apt-get install libtinfo5
终于尼玛的完成了
接下来编辑my.cnf,在mysqld下面黏贴几个玩意
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
重启docker
docker restart mysql
终于正常了,佛了
设置mysql远程连接
查看用户权限
mysql> select host,user,plugin,authentication_string from user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$f&/uW)ZX LcE%"f3mdndFClPzqRACxH9vTuOk90Gs12jnuKt0k4Ens9IB |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$k{`vW;lca|%)f~lr[T6ot4.MCPhmNFHKzti8SS0TwxMgcubqb2l89hTIlr44 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
这个镜像好像默认开启了root的远程连接权限,但是却无法用navicat登工具进行连接
mysql8默认的身份验证插件为caching_sha2_password
,该插件不能使用旧版本的客户端来连接(也许新的第三方客户端可以连接该插件),粘贴一份mysql官方的说明
If your MySQL installation must serve pre-8.0 clients and you encounter compatibility issues after upgrading to MySQL 8.0 or higher, the simplest way to address those issues and restore pre-8.0 compatibility is to reconfigure the server to revert to the previous default authentication plugin (
mysql_native_password
). For example, use these lines in the server option file:[mysqld] default_authentication_plugin=mysql_native_password
That setting enables pre-8.0 clients to connect to 8.0 servers until such time as the clients and connectors in use at your installation are upgraded to know about
caching_sha2_password
. However, the setting should be viewed as temporary, not as a long term or permanent solution, because it causes new accounts created with the setting in effect to forego the improved authentication security provided bycaching_sha2_password
.
故修改插件为mysql_native_passwordALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘你的密码’;
大功告成