数据库教程:Docker安装Mysql5.7

Docker安装 系统版本:CentOS Linux release 7.5.1804 (Core) 如果之前有装过docker可以先卸载旧版本再安装,没有则跳过。 yum remove docker docker-client docker-client-latest docker-c …


docker安装

系统版本:centos linux release 7.5.1804 (core)

如果之前有装过docker可以先卸载旧版本再安装,没有则跳过。

yum remove docker                    docker-client                    docker-client-latest                    docker-common                    docker-latest                    docker-latest-logrotate                    docker-logrotate                    docker-engine

 

安装软件包:

yum install -y yum-utils  device-mapper-persistent-data  lvm2

 

设置镜像仓库:

yum-config-manager   --add-repo   https://download.docker.com/linux/centos/docker-ce.repo

 

安装docker:

yum install -y docker-ce docker-ce-cli containerd.io

 

启动docker:

systemctl start docker

 

可以进行测试:

# docker run hello-world
unable to find image ‘hello-world:latest’ locally
latest: pulling from library/hello-world
1b930d010525: pull complete
digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
status: downloaded newer image for hello-world:latest

hello from docker!
this message shows that your installation appears to be working correctly.

to generate this message, docker took the following steps:
1. the docker client contacted the docker daemon.
2. the docker daemon pulled the “hello-world” image from the docker hub.
(amd64)
3. the docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. the docker daemon streamed that output to the docker client, which sent it
to your terminal.

to try something more ambitious, you can run an ubuntu container with:
$ docker run -it ubuntu bash

share images, automate workflows, and more with a free docker id:
https://hub.docker.com/

for more examples and ideas, visit:
https://docs.docker.com/get-started/

 

安装mysql5.7

可以看下镜像库里的mysql相关镜像:

# docker search mysql name                              description                                     stars               official            automated mysql                             mysql is a widely used, open-source relation…   9191                [ok] mariadb                           mariadb is a community-developed fork of mys…   3273                [ok] mysql/mysql-server                optimized mysql server docker images. create…   679                                     [ok] centos/mysql-57-centos7           mysql 5.7 sql database server                   70 mysql/mysql-cluster               experimental mysql cluster docker images. cr…   63 centurylink/mysql                 image containing mysql. optimized to be link…   61                                      [ok] deitch/mysql-backup               replaced! please use https://hub.docker.com/r…   41                                      [ok] bitnami/mysql                     bitnami mysql docker image                      36                                      [ok] tutum/mysql                       base docker image to run a mysql database se…   34 schickling/mysql-backup-s3        backup mysql to s3 (supports periodic backup…   29                                      [ok] prom/mysqld-exporter                                                              27                                      [ok] linuxserver/mysql                 a mysql container, brought to you by linuxse…   24 centos/mysql-56-centos7           mysql 5.6 sql database server                   19 circleci/mysql                    mysql is a widely used, open-source relation…   19 mysql/mysql-router                mysql router provides transparent routing be…   14 arey/mysql-client                 run a mysql client from a docker container      13                                      [ok] databack/mysql-backup             back up mysql databases to... anywhere!         10 openshift/mysql-55-centos7        deprecated: a centos7 based mysql v5.5 image…   6 fradelg/mysql-cron-backup         mysql/mariadb database backup using cron tas…   5                                       [ok] genschsa/mysql-employees          mysql employee sample database                  4                                       [ok] devilbox/mysql                    retagged mysql, mariadb and perconadb offici…   2 ansibleplaybookbundle/mysql-apb   an apb which deploys rhscl mysql                2                                       [ok] jelastic/mysql                    an image of the mysql database server mainta…   1 monasca/mysql-init                a minimal decoupled init container for mysql    0 widdpim/mysql-client              dockerized mysql client (5.7) including curl…   0                                       [ok]

第一条就是官方的镜像,目前是8.0版本,由于我们需要的是5.7的所以要带上版本号来拉镜像。

# docker pull mysql:5.7 5.7: pulling from library/mysql image docker.io/library/mysql:5.7 uses outdated schema1 manifest format. please upgrade to a schema2 image for better future compatibility. more information at https://docs.docker.com/registry/spec/deprecated-schema-v1/ 68ced04f60ab: downloading f9748e016a5c: downloading da54b038fed1: downloading 6895ec5eb2c0: downloading 111ba0647b87: downloading c1dce60f2f1a: downloading 702ec598d0af: downloading 63cca87a5d4d: downloading ec05b7b1c5c7: downloading 834b1d9f49b0: downloading 8ded6a30c87c: downloading 5.7: pulling from library/mysql 68ced04f60ab: pull complete f9748e016a5c: pull complete da54b038fed1: pull complete 6895ec5eb2c0: pull complete 111ba0647b87: pull complete c1dce60f2f1a: pull complete 702ec598d0af: pull complete 63cca87a5d4d: pull complete ec05b7b1c5c7: pull complete 834b1d9f49b0: pull complete 8ded6a30c87c: pull complete digest: sha256:f4a5f5be3d94b4f4d3aef00fbc276ce7c08e62f2e1f28867d930deb73a314c58 status: downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7

 

查看本地镜像

# docker images repository          tag                 image id            created             size mysql               5.7                 84164b03fa2e        15 hours ago        456mb hello-world         latest              fce289e99eb9        14 months ago       1.84kb

可以看到版本是5.7

 

创建所需目录,用以挂载外部配置文件和数据目录:

mkdir /data mkdir /data/mysql3306 mkdir /data/mysql3306/conf.d mkdir /data/mysql3306/data/

 

编辑配置文件,以下配置文件仅供参考:

# vim /data/mysql3306/my.cnf [client] port= 3306 socket  = /tmp/mysql.sock #default-character-set = utf8mb4  ## the mysql server [mysqld] port = 3306 socket  = /tmp/mysql.sock user = mysql skip-external-locking skip-name-resolve #skip-grant-tables #skip-networking ###################################### dir #basedir=/usr/local/mysql datadir=/var/lib/mysql tmpdir=/var/lib/mysql secure_file_priv=/var/lib/mysql ###################################### some app log-error=mysql.err pid-file=/var/lib/mysql/mysql.pid local-infile=1 event_scheduler=0 federated default-storage-engine=innodb #default-time-zone= '+8:00' log_timestamps=system character-set-client-handshake = false character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='set names utf8mb4' #5.6 explicit_defaults_for_timestamp=true  #fulltext innodb_optimize_fulltext_only ft_min_word_len=1 #ft_max_word_len innodb_ft_min_token_size=1  ###################################### memory allocate and myisam configure max_connections=3000 #back_log=200 max_connect_errors=10000  key_buffer_size = 16m max_allowed_packet = 16m table_open_cache = 10240 sort_buffer_size = 2m read_buffer_size = 2m read_rnd_buffer_size = 2m join_buffer_size=2m myisam_sort_buffer_size = 4m #net_buffer_length = 2m thread_cache_size = 24  query_cache_type=1 query_cache_size=256m query_cache_limit=32m  tmp_table_size=1g max_heap_table_size=1g  #thread_concurrency =48 ###################################### replication server-id = 10951 log-bin=mysql-bin binlog_format=mixed max_binlog_size=1g #binlog_cache_size=512m log_slave_updates=true log_bin_trust_function_creators=true expire_logs_days=15 replicate-ignore-db=mysql replicate-ignore-db=test replicate-ignore-db=information_schema replicate-ignore-db=performance_schema replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% replicate-wild-ignore-table=performance_schema.%  lower_case_table_names = 1 #read_only=1 master_info_repository=table relay_log_info_repository=table  ###################################### slow-query long_query_time=1 slow_query_log=1 slow_query_log_file=/var/lib/mysql/slow-query.log interactive_timeout=600 wait_timeout=600 #log_queries_not_using_indexes=1  ###################################### innodb configure innodb_file_per_table #innodb_file_format=barracuda #innodb_io_capacity=200  innodb_data_home_dir = /var/lib/mysql #innodb_data_file_path = ibdata1:2000m;ibdata2:10m:autoextend innodb_log_group_home_dir = /var/lib/mysql innodb_buffer_pool_size =4g # set .._log_file_size to 25 % of buffer pool size innodb_log_file_size = 1g innodb_log_files_in_group = 3 innodb_log_buffer_size = 32m #innodb_lock_wait_timeout = 50 innodb_flush_log_at_trx_commit = 1 sync_binlog=0 sql-mode="strict_trans_tables,no_auto_create_user,no_engine_substitution" ########################################## [mysqldump] quick max_allowed_packet = 16m  [mysql] no-auto-rehash default-character-set = utf8mb4 prompt=\u \h \r:\m:\s \d>  [myisamchk] key_buffer_size = 20m sort_buffer_size = 20m read_buffer = 2m write_buffer = 2m  [mysqlhotcopy] interactive-timeout

 

开始启动容器

# docker run --restart=always --privileged=true -v /etc/localtime:/etc/localtime:ro -d -v /data/mysql3306/data/:/var/lib/mysql -v /data/mysql3306/conf.d:/etc/mysql/conf.d -v /data/mysql3306/my.cnf:/etc/mysql/my.cnf -p 3306:3306 --name mysql5.7 -e mysql_root_password=123456 mysql:5.7

参数说明:
–restart=always: 当docker 重启时,容器会自动启动。
–privileged=true:容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
-v /opt/mysql/conf.d/my.cnf:/etc/my.cnf:映射配置文件
-v /opt/mysql/data/:/var/lib/mysql:映射数据目录
-v /etc/localtime:/etc/localtime:ro:设置容器的时间与宿主机同步
-p 3306:3306:将容器的3306端口映射到主机的3306端口
-e mysql_root_password=123456:设置mysql的root密码

查看容器状态:

# docker ps -a container id        image               command                  created             status                   ports            names 5aa35cacab13        mysql:5.7           "docker-entrypoint.s…"   20 minutes ago      up 20 minutes            0.0.0.0:3306->3306/tcp,33060/tcp   mysql5.7 3d60c83336e8        hello-world         "/hello"                 2 hours ago         exited (0) 2 hours ago            vibrant_wescoff

 

进入容器

# docker exec -it 5aa35cacab13 /bin/bash root@5aa35cacab13:/#

验证配置文件是否生效:

root@5aa35cacab13:/# mysql -uroot -p123456 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 3 server version: 5.7.29-log 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.  root@localhost localhost 17:13:48 (none)>select @@sql_mode; +----------------------------------------------------------------+ | @@sql_mode                                                     | +----------------------------------------------------------------+ | strict_trans_tables,no_auto_create_user,no_engine_substitution | +----------------------------------------------------------------+ 1 row in set (0.00 sec)

可以看到和我们配置文件里设置的一样。

 

需要了解更多数据库技术:Docker安装Mysql5.7,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/821306.html

(0)
上一篇 2021年9月16日
下一篇 2021年9月16日

精彩推荐