开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
查看镜像
docker search mariadb
拉取镜像
docker pull mariadb:10.6.10
创建配置文件
mkdir -p /docker_data/mariadb/conf
vim /docker_data/mariadb/conf/my.cnf
[mysqld]
character-set-server=utf8mb4
启动mariadb
docker run -d -p 3306:3306 --privileged=true --restart=always -v /docker_data/mariadb/log:/var/log/mysql -v /docker_data/mariadb/data:/var/lib/mysql -v /docker_data/mariadb/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mariadb-10.6.10 mariadb:10.6.10
exec进入容器,登录mariadb,创建用户和密码,授权,刷新。
docker exec -it mariadb-10.6.10 bash
mysql -u root -p
#创建用户名为x密码为x的用户授权全权限WITH GRANT OPTION表示可以给其它用户授权
GRANT ALL PRIVILEGES ON *.* TO 'x'@'%' IDENTIFIED BY 'x'WITH GRANT OPTION;
#刷新
FLUSH PRIVILEGES;
#退出mariadb
exit;
#退出容器
exit
正确示例如下:
[root@mariadb ~]# docker exec -it mariadb-10.6.10 bash
root@fd5efab0255a:/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.10-MariaDB-1:10.6.10+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 USER 'x'@'%' IDENTIFIED BY 'x';
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> GRANT all on *.* to 'x'@'%';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> exit;
Bye
root@fd5efab0255a:/# exit
exit