我们自己在linux上安装mysql的话,超级麻烦,安装完后还要进行各种各样的配置,之前在docker专题写了用docker安装mysql的快速版本以及修改编码的方式,这里总结一下安装流程和命令。
环境
Linux version 3.10.0-693.2.2.el7.x86_64
docker18.09.2
mysql5.7
安装流程命令
下面我直接快速的说明安装流程和命令,包括中文编码问题的解决。
1、拉取镜像
docker pull mysql:5.7
2、创建配置和数据挂载点
mkdir -p /data/mysql/data3309
mkdir -p /data/mysql/mysql.conf.d3309
这里启动的容器端口为3309,所以直接命名为3309
3、编写配置文件
新建名称为mysqld.cnf的配置文件,访入/data/mysql/mysql.conf.d3309/目录下,内容如下,目的是覆盖掉容器内的配置文件,好修改编码格式为utf_8,而不是参照网上的进入容器中修改。
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#innodb_buffer_pool_size = 50M
#ngram_token_size=1
#ft_min_word_len=3
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
顺便说下,里面的这些配置,其实跟mysql的快速安装没有关系。主要是加上设置编码的内容,就是后面两句
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
innodb_buffer_pool_size默认是128M
ngram_token_size默认分词是2,用于全文索引中文分词
ft_min_word_len默认值是4,只有四个字母的单词才会建立全文索引
4、创建mysql容器
这里创建的容器端口为3309,root密码为123456,数据和配置文件挂载点用上面创建的目录,设置编码格式为utf8mb4
docker create --name mysql3309 -v /data/mysql/data3309:/var/lib/mysql -v /data/mysql/mysql.conf.d3309/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -e MYSQL_ROOT_PASSWORD=123456 -p 3309:3306 mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
5、启动容器
docker start mysql3309
有时候启动会报错
Error response from daemon: driver failed programming external connectivity on endpoint mysql3309 (de08259bae3ad8bb7ea5fdff3adcbb3f84e5258a892652f0489d3e3d33aab46c): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3309 -j DNAT --to-destination 172.17.0.7:3306 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1))
Error: failed to start containers: mysql3309
这里我们只需要重启docker就可以了
systemctl restart docker
6、测试容器
首先进入到容器
docker exec -it mysql3309 bash
然后用如下命令登录,当然也可以省去-h127.0.0.1
mysql -h127.0.0.1 -uroot -p123456
若是进入到mysql命令行界面就表明安装成功
7、新建另一个mysql容器
只需要新建挂载点然后执行创建mysql容器命令即可。(注意端口号的修改)
8、一些操作命令
docker stop mysql3309 #停止容器
docker start mysql3309 #启动容器
docker exec -it mysql3309 bash #进入容器
docker search name #查询镜像
docker pull name #安装name镜像
docker images #查看已经拉取的镜像
docker ps #查看正在运行中的容器
docker ps -a #查看正在运行中的容器,包括未运行的
docker rm #容器ID 删除某一容器 //停止容器--》删除容器--》删除镜像
docker rmi #镜像ID 删除某一个镜像
systemctl start docker #启动docker
systemctl stop docker #停止docker
systemctl restart docker #重启docker