本文章主要介绍 如何使用数据卷,备份docker容器数据库的数据。默认宿主设备已安装好docker.
在宿主设备上创建目录,一个配置文件my.cnf。 目录将作为容器内数据库实际数据和日志数据的数据卷。
创建完成后,如下所示
[root@yj138 mysqlbak]# ls -a
. .. data my.cnf
[root@yj138 mysqlbak]#
修改配置文件内容 my.cnf, 这个文件起始也不需要改变,如果构建镜像时,已经修改,且反之在 /etc/my.cnf下。其实,在安装过程中,该文件默认在 安装路下, 只要mysqld 配置了basedir参数,可不用将该文件拷贝到/etc/目录下。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[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 =/usr/local/mysql/data/mysql.pid
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
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
docker run -tdi -v /mysqlbak/data:/use/local/mysql/data -v /mysqlbak/my.cnf:/etc/my.cnf --name mysql IMAGEID
启动容器后,进入容器内部
docker exec -ti mysql /bin/bash
该步骤字需要初始化一次(如果挂载的数据库备份目录不变),如果每次启动挂载不同的目录,则启动容器后,需要初始化,不然会爆出如下错误。同时需要注意 myql的数据目录的权限应该是 mysql:mysql(这是自己创建的,也可是其他用户,但注意在数据库初始化是需要指定该用户名)
root@6aae6b71082e:/usr/local/mysql# service mysqld start
Starting MySQL
. * The server quit without updating PID file (/usr/local/mysql/data/6aae6b71082e.pid).
root@6aae6b71082e:/usr/local/mysql#
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql.pid
数据库初始化,需要进入到目录 script , 然后执行上述指令。
执行完成后,发现 data 目录下新建了内容,同时在宿主设备对应的目下,也发现相同内容
root@6aae6b71082e:/usr/local/mysql/scripts# cd ..
root@6aae6b71082e:/usr/local/mysql# cd data && ls -a
. .. 6aae6b71082e.err auto.cnf ib_logfile0 ib_logfile1 ibdata1 mysql performance_schema test
root@6aae6b71082e:/usr/local/mysql/data# exit
[root@yj138 mysqlbak]# cd data && ls -a
. .. 6aae6b71082e.err auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
[root@yj138 data]#
完成上述后,可启动容器内数据库
root@6aae6b71082e:/usr/local/mysql/data# service mysqld start
Starting MySQL
. *
root@6aae6b71082e:/usr/local/mysql/data#
查看下启动状态
root@6aae6b71082e:/usr/local/mysql/data# service mysqld status
* MySQL running (452)
root@6aae6b71082e:/usr/local/mysql/data#
尝试添加数据库,添加数据,看外部是否有备份
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> use test1;
mysql> CREATE TABLE userInfo( userId int not null, usename varchar(50), userage int, primary key(userId)); Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO userInfo VALUES(1,'test', 20);
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables
-> ;
+-----------------+
| Tables_in_test1 |
+-----------------+
| userInfo |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from userInfo;
+--------+---------+---------+
| userId | usename | userage |
+--------+---------+---------+
| 1 | test | 20 |
+--------+---------+---------+
1 row in set (0.00 sec)
mysql>
查看下外部数据
[root@yj138 data]# ls -a
. .. 6aae6b71082e.err 6aae6b71082e.pid auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test test1
[root@yj138 data]# cd test1 && ls -a
. .. db.opt userInfo.frm userInfo.ibd
[root@yj138 test1]#
bingo 。 如果需要确保容器断开就重启, 在docker run 启动容器时,添加参数 --restart=always
下次重点,研究如何将数据和日志分开存储。