本文演示部署mariadb:10.5.18容器,并进行数据持久化配置。
一、初始化容器
1、导入容器镜像
[root@localhost opt]# docker load -i mariadb-10.5.18.tar
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
Getting image source signatures
Copying blob 49a0399cd18c done
Copying blob 493750321108 done
Copying blob 9da31ba8bed9 done
Copying blob f7e62cac0998 done
Copying blob 0002c93bdb37 done
Copying blob de5c8c2fcb3e done
Copying blob 42df24b5093f done
Copying blob a9c8a0c876d0 done
Copying config 819fb13996 done
Writing manifest to image destination
Storing signatures
Loaded image: localhost/mariadb:10.5.18
2、生成容器实例并初始化root帐户密码
[root@localhost opt]#docker run -itd --restart=always --name mysqldb -e MARIADB_ROOT_PASSWORD=RmBw@a0tI -d -p 3306:3306 mariadb:10.5.18
二、数据库数据持久化
1、拷贝数据库数据目录到本地目录
[root@localhost dockercnf]# docker cp mysqldb:/var/lib/mysql /data
[root@localhost dockercnf]# mv /data/mysql /data/mysqldata
2、拷贝数据库配置文件到本地
[root@localhost dockercnf]# docker cp mysqldb:/etc/mysql/mariadb.cnf /data/mywebcnf/mariadb.cnf
[root@localhost dockercnf]# chmod a+rX /data/mywebcnf -R
3、删除现有容器实例,用本地数据、本地配置文件重新生成容器
root@localhost dockercnf]# docker rm -f mysqldb
mysqldb
[root@localhost dockercnf]# docker run -itd --restart=always --name mysqldb -v /data/mysqldata:/var/lib/mysql -v /data/mywebcnf/mariadb.cnf:/etc/mysql/mariadb.cnf -d -p 3306:3306 mariadb:10.5.18
688187f72efebd9015b9d24e9a203627f362bf7482c3dd65910a36c05fe366a0
三、验证效果
1、宿主机安装数据库客户端程序 ,并用之前初始化的root帐号尝试登录
[root@localhost data]# dnf install mariadb
[root@localhost data]# mysql -uroot -p -h127.0.0.1
2、可用之前初始化的root密码正常登入数据库,数据持久化工作完成。

四、创建测试库
1、创建测试库及用户帐号
[root@localhost data]# mysql -h10.227.17.241 -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.18-MariaDB-1:10.5.18+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE cloud_platform character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON cloud_platform.* TO cpdb@'%' IDENTIFIED BY 'OmyGod!2012';
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
2、查看测试库
MariaDB [(none)]> show databases;

3、退出后用新建用户帐号登录
[root@localhost data]# mysql -h10.227.17.241 -ucpdb -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.18-MariaDB-1:10.5.18+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;

业务测试库及相关帐号生成正常。



被折叠的 条评论
为什么被折叠?



