之前一篇随笔《Docker Centos安装Openssh》 写的是如何在基础的centos镜像中搭建ssh服务,在此基础上再搭建其他服务。本文继续介绍在centos_ssh基础上搭建mysql服务。
1、下载mariaDB
下载地址:http://yum.mariadb.org/10.0.33/centos7-amd64/rpms/
下载以下四个包:
MariaDB-10.0.33-centos7-x86_64-server.rpm
MariaDB-10.0.33-centos7-x86_64-common.rpm
MariaDB-10.0.33-centos7-x86_64-client.rpm
MariaDB-10.0.33-centos7-x86_64-compat.rpm
2、本文将把mariadb的数据目录使用宿主机上
mkdir /mnt/data/mysql
目录 /mnt/data/mysql 也可以是单独逻辑卷的挂载目录,由自己决定;逻辑卷的使用不属于此文内容;
3、将上面下载的四个mariaDB安装包拷贝到容器中,第一步
此处是先借用了/mnt/data/mysql/,将四个mariaDB包拷贝到宿主机的/mnt/data/mysql/中;
4、启动centos_sshd镜像
#docker run --net=host -d --name=mariadb -it -v /mnt/data/mysql/:/var/lib/mysql:z -p 3306:3306 mariadb:10.0.33 /run.sh
这里用的是host模式连接的网络,启动之后即可通过ssh登录到容器内部,装上mysql之后可以直接重启容器来验证是否成功;-v参数后面带“:z”后缀来理发SELinux标签。
5、将上面下载的四个mariaDB安装包拷贝到容器中,第二步
先进入第4步中运行的容器mariadb,在宿主机上运行下面的命令,系统进入容器
#docker exec -it mariadb /bin/bash
然后,将第3步中拷贝到/mnt/data/mysql/的mariadb包拷贝到容器中的 ~/ 下,执行如下命令:
#mv /mnt/data/mysql/* ~/
#cd
# ll
total 68848
-rw-r--r--. 1 root root 10457036 Jan 6 05:50 MariaDB-10.0.33-centos7-x86_64-client.rpm
-rw-r--r--. 1 root root 43692 Jan 6 05:50 MariaDB-10.0.33-centos7-x86_64-common.rpm
-rw-r--r--. 1 root root 1499852 Jan 6 05:50 MariaDB-10.0.33-centos7-x86_64-compat.rpm
-rw-r--r--. 1 root root 58489196 Jan 6 05:50 MariaDB-10.0.33-centos7-x86_64-server.rpm
6、安装MariaDB
# yum remove mysql-libs -y
# yum -y install *.rpm
# yum clean all
# chown -R mysql:mysql /dev
7、开启MariaDB
# mysqld_safe &
5、配置root用户密码、赋予root远程连接权限
再开一个终端,进入mysql,执行如下命令:
# mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('123456') WHERE User = 'root';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> flush privileges;
6、更改mysql配置,使服务忽略大小写、以utf8传输等
# vi /etc/my.cnf.d/server.cnf
[mysqld]
bind-address=0.0.0.0
console=1
general_log=1
general_log_file=/dev/stdout
log_error=/dev/stderr
collation-server=utf8_unicode_ci
character-set-server=utf8
如果想做其它配置信息,可自行添加
7、更改启动脚本
[root@localhost /]# vi /run.sh
#!/bin/bash
chgrp mysql /dev
chmod g+w /dev
#通过检测/var/lib/mysql/mysql是否存在,来确定是否要初始化数据库
if [ ! -d "/var/lib/mysql/mysql" ]; then
mysql_install_db --user=mysql > /dev/null
fi
if [ ! -d "/var/run/mysqld" ]; then
mkdir /var/run/mysqld
fi
chgrp mysql /var/run/mysqld
chmod g+w /var/run/mysqld
mysqld_safe &
/usr/sbin/sshd -D
上面只所以要做数据库的初始化是考虑上面第4步中新建目录/mnt/data/mysql/无数据的情况;
8、重启容器
[root@localhost home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a74714adeccd centos "/run.sh" 17 minutes ago Up 17 minutes mariadb
[root@localhost home]# docker restart mariadb
9、验证mysql,在其余安装了mysql的服务器上执行:
[root@localhost home]# mysql -u root -p123456 -h192.168.11.130
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
[root@localhost home]# mysql -u root -p123456 -h192.168.31.203
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql>
10、提交至docker镜像,该步骤在《Docker Centos安装Openssh》中提到过,敬请移步至该文章。
本文是在《Docker Centos安装Mysql5.6》的基础上,结合自己的操作情况作了调整后形成的文章