docker容器内安装mysql

  本文介绍docker 容器内安装 mysql

安装环境  Ubuntu 16.04     docker-ce 18.09 

一、获取源码包

下载地址   https://www.mysql.com/downloads/

选择需要的版本, 这里选择的5.6.33,通用版,linux下64位

启动容器并挂载安装包目录到容器内部目录

run -tid --rm --dns 8.8.8.8 --dns 8.8.4.4 -v 主机源码包目录:容器内目录 --name 容器名称 镜像ID

二、容器内容解压并安装

 进入容器内

docker exec -ti 容器名  /bin/bash

进入具体目录,使用 tar  解压

 tar -zvxf mysql-5.6.23.tar.gz -C /mnt
root@d970e0c9e197:/# cd /mnt
root@d970e0c9e197:/mnt# ls -a
.  ..  mysql-5.6.23
root@d970e0c9e197:/mnt# cd mysql-5.6.23
root@d970e0c9e197:/mnt/mysql-5.6.23# ls -a
.            CMakeFiles           INSTALL-SOURCE      client          configure        libevent     mysql-test  regex       storage        vio
..           CMakeLists.txt       INSTALL-WIN-SOURCE  cmake           configure.cmake  libmysql     mysys       scripts     strings        win
.gitignore   COPYING              README              cmd-line-utils  dbug             libmysqld    mysys_ssl   sql         support-files  zlib
BUILD        Docs                 VERSION             config.h        extra            libservices  packaging   sql-bench   tests
BUILD-CMAKE  Doxyfile-perfschema  VERSION.dep         config.h.cmake  include          man          plugin      sql-common  unittest
root@d970e0c9e197:/mnt/mysql-5.6.23# 

注意在目录内   config.h.cmake  。 去掉.cmake.  并备份

修改源码  vi ./cmd-line-utils/libedit/terminal.c    865-880 行之间

注释 char buff[TC_BUFFSIZE] ;  并将 area置为NULL  

protected int 
terminal_set(EditLine *el, const char *term) 
{ 
int i; 
/*char buf[TC_BUFSIZE];*/ 
char *area; 
const struct termcapstr *t; 
sigset_t oset, nset; 
int lins, cols; 
--phpfensi.com 
(void) sigemptyset(&nset); 
(void) sigaddset(&nset, SIGWINCH); 
(void) sigprocmask(SIG_BLOCK, &nset, &oset); 
 
area = NULL; 

2.1 安装必要工具  cmake   bison

另外:Debian/Ubuntu 下安装 libncurses5-dev    redhat下安装: ncurses-devel

apt-get install cmake 
apt-get install bison
apt-get install libncurses5-dev  

2.2 cmake 编译mysql

进入目录 /mnt/mysql-5.6.23

root@d970e0c9e197:/mnt/mysql-5.6.23# cmake -DMAKE_INSTALL_PREFIX=/usr/local/mysql

,,,
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/mysql-5.6.23
root@d970e0c9e197:/mnt/mysql-5.6.23# 

执行编译过程,  然后等待编译

root@d970e0c9e197:/mnt/mysql-5.6.23# make && make install
...
...

[ 22%] Built target myisam_ftdump
Scanning dependencies of target myisam_embedded
[ 22%] Building C object storage/myisam/CMakeFiles/myisam_embedded.dir/ft_boolean_search.c.o
[ 22%] Building C object storage/myisam/CMakeFiles/myisam_embedded.dir/ft_nlq_search.c.o
[ 22%] Building C object storage/myisam/CMakeFiles/myisam_embedded.dir/ft_parser.c.o

...

2.3 创建用户与用户组

编译完成后,可在路径 /usr/local/mysql 查看。 安装完成

root@d970e0c9e197:/usr/local/mysql# ls -a
.  ..  COPYING  INSTALL-BINARY  README  bin  data  docs  include  lib  man  mysql-test  scripts  share  sql-bench  support-files
root@d970e0c9e197:/usr/local/mysql# 

创建用户组 mysql  并添加用 mysql

root@d970e0c9e197:/usr/local/mysql# groupadd mysql
root@d970e0c9e197:/usr/local/mysql# useradd -g mysql mysql
root@d970e0c9e197:/usr/local/mysql# 

添加用户密码   自定义 例如 yaspeed123

root@d970e0c9e197:/usr/local/mysql# passwd mysql
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@d970e0c9e197:/usr/local/mysql# 
root@d970e0c9e197:/usr/local/mysql# 

2.4 安装mysql  并指定用户 mysql

root@d970e0c9e197:/usr/local/mysql# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/data

安装完成后,查看目录信息,发现data目录的权限改为 mysql  mysql下

root@d970e0c9e197:/usr/local/mysql# ls -al
total 168
drwxr-xr-x. 13 root  root    4096 Nov 29 09:50 .
drwxr-xr-x. 13 root  root    4096 Nov 29 09:50 ..
-rw-r--r--.  1 root  root   17987 Jan 19  2015 COPYING
-rw-r--r--.  1 root  root  102315 Jan 19  2015 INSTALL-BINARY
-rw-r--r--.  1 root  root    2496 Jan 19  2015 README
drwxr-xr-x.  2 root  root    4096 Nov 29 09:22 bin
drwxr-xr-x.  5 mysql mysql    104 Nov 29 09:47 data
drwxr-xr-x.  2 root  root      52 Nov 29 09:22 docs
drwxr-xr-x.  3 root  root    4096 Nov 29 09:22 include
drwxr-xr-x.  3 root  root    4096 Nov 29 09:22 lib
drwxr-xr-x.  4 root  root      28 Nov 29 09:22 man
-rw-r--r--.  1 root  root     943 Nov 29 09:50 my-new.cnf
-rw-r--r--.  1 root  root     943 Nov 29 09:47 my.cnf
drwxr-xr-x. 10 root  root    4096 Nov 29 09:22 mysql-test
drwxr-xr-x.  2 root  root      29 Nov 29 09:22 scripts
drwxr-xr-x. 28 root  root    4096 Nov 29 09:22 share
drwxr-xr-x.  4 root  root    4096 Nov 29 09:22 sql-bench
drwxr-xr-x.  2 root  root    4096 Nov 29 09:22 support-files
root@d970e0c9e197:/usr/local/mysql# 

2.5 配置开机启动服务

修改文件 /support-files/mysql.server 

docker容器内安装mysql_第1张图片

然后拷贝该文件到  /etc/init.d/   并修改启动权限

root@d970e0c9e197:/usr/local/mysql# cp support-files/mysql.server /etc/init.d/mysqld
root@d970e0c9e197:/usr/local/mysql# chmod 755 /etc/init.d/mysqld
root@d970e0c9e197:/usr/local/mysql# 

修改目录 mysql 目录下 my.conf配置

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /usr/local/mysql/data/mysql.sock

[mysql]
default-character-set=utf8
socket = /usr/local/mysql/data/mysql.sock
#socket = /tmp/mysql.sock

[client]
default-character-set=utf8
socket = /usr/local/mysql/data/mysql.sock

拷贝到 /etc 下

root@d970e0c9e197:/usr/local/mysql# cp my.cnf /etc/my.cnf
root@d970e0c9e197:/usr/local/mysql# 

然后启动服务

service mysqld start

查看启动状态

root@d970e0c9e197:/# ps -ef | grep mysqld
root     21102     0  0 10:36 pts/3    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/d970e0c9e197.pid
mysql    21295 21102  0 10:36 pts/3    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/d970e0c9e197.err --pid-file=/usr/local/mysql/data/d970e0c9e197.pid --socket=/usr/local/mysql/data/mysql.sock --port=3306
root     21330 21317  0 11:06 pts/4    00:00:00 grep --color=auto mysqld
root@d970e0c9e197:/# netstat -ano | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      off (0.00/0/0)
root@d970e0c9e197:/# 
root@d970e0c9e197:/# service mysqld status
 * MySQL running (21295)
root@d970e0c9e197:/# 

安装完成后, 别忘了 使用 docker commit  提交容器为镜像, 保存起来

你可能感兴趣的:(DOCKER,Docker容器学习)