文章目录
1. 安装docker
这里以centos为例进行说命名如何安装docker:
-
首先使用
uname -r命令查看内核版本,确保是3.10及以上[root@izbp15ffbqqbe97j9dcf5dz ~]# uname -r 3.10.0-514.26.2.el7.x86_64 -
更新
yum update -
使用
yum install docker命令安装docker[root@izbp15ffbqqbe97j9dcf5dz ~]# yum install docker Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Loading mirror speeds from cached hostfile Package 2:docker-1.13.1-161.git64e9980.el7_8.x86_64 already installed and latest version Nothing to do -
使用
systemctl start docker命令启动docker[root@izbp15ffbqqbe97j9dcf5dz ~]# systemctl start docker [root@izbp15ffbqqbe97j9dcf5dz ~]# docker -v Docker version 1.13.1, build 64e9980/1.13.1 -
使用命令
systemctl enable docker设置docker为开机自启[root@izbp15ffbqqbe97j9dcf5dz ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. -
如果要停止docker运行,可以使用
systemctl stop docker
2. 安装MySQL
2.1 docker安装MySQL
-
拉取镜像:
docker pull mysql,这里选择默认的最新镜像[root@izbp15ffbqqbe97j9dcf5dz ~]# docker pull mysql Using default tag: latest Trying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysql 8559a31e96f4: Pull complete d51ce1c2e575: Pull complete c2344adc4858: Pull complete fcf3ceff18fc: Pull complete 16da0c38dc5b: Pull complete b905d1797e97: Pull complete 4b50d1c6b05c: Pull complete c75914a65ca2: Pull complete 1ae8042bdd09: Pull complete 453ac13c00a3: Pull complete 9e680cd72f08: Pull complete a6b5dc864b6c: Pull complete Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 Status: Downloaded newer image for docker.io/mysql:latest -
端口映射:
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql*使用
docker run ‐‐name some‐mysql ‐e MYSQL_ROOT_PASSWORD=my‐secret‐pw ‐d mysql:tag ‐‐character‐setserver=utf8mb4 ‐‐collation‐server=utf8mb4_unicode_ci可以指定mysql的一些配置参数
2.2 docker-compose安装MySQL
docker-compose的安装可见:Docker入门简易教程 – 通俗易懂 +足够使用
首先在当前目录下创建两个目录,便于后续的目录映射:
mkdir data
mkdir conf
进入conf目录,编写 mymysqld.cnf :
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
在当前目录下创建docker-compose.yml文件,内容如下:
[root@iZbp15ffbqqbe97j9dcf5dZ docker_mysql]# cat docker-compose.yml
version: '3'
services:
mysql_compose:
image: daocloud.io/library/mysql:8.0.2
container_name: mymysql
restart: always
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
- ./conf/mymysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
environment:
MYSQL_ROOT_PASSWORD: "123456"
最后使用命令docker-compose up -d启动容器,并使用docker ps可以查看容器是否正常启动:
[root@iZbp15ffbqqbe97j9dcf5dZ docker_mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cfaa4a79efb daocloud.io/library/mysql:8.0.2 "docker-entrypoint.s…" 7 hours ago Up 7 hours 0.0.0.0:3306->3306/tcp mymysql
3. 配置MySQL
-
使用
docker ps查看运行中容器的id[root@iZbp15ffbqqbe97j9dcf5dZ docker_mysql]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfaa4a79efb daocloud.io/library/mysql:8.0.2 "docker-entrypoint.s…" 7 hours ago Up 7 hours 0.0.0.0:3306->3306/tcp mymysql -
进入容器:
docker exec -it 容器id bash -
进入mysql
mysql -uroot -p -
对远程连接进行授权
GRANT ALL ON *.* TO 'root'@'%'; -
更改密码的加密规则
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; -
更改root密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; -
刷新权限
flush privileges; -
查看用户信息
select host,user,plugin,authentication_string from mysql.user;
4. 云服务器设置
除了进行数据库的相关设置外,还需要配置服务器的安全规则。点击云控制平台左侧的网络与安全 --> 安全—> 配置规则
然后按照以下方式手动添加安全规则
5. 配置客户端连接
首先需配置SSH通道
配置数据库连接的端口号、用户名和密码
连接成功!