Docker 部署 mysql
1、启动 mysql 容器
[root@localhost ~]# docker run --name mysql -v /mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Root@1234 -p 3306:3306 -d mysql
[root@localhost ~]# ls /mysqldata/
auto.cnf binlog.index client-cert.pem '#ib_16384_1.dblwr' ibtmp1 mysql mysql_upgrade_history public_key.pem sys
binlog.000001 ca-key.pem client-key.pem ib_buffer_pool '#innodb_redo' mysql.ibd performance_schema server-cert.pem undo_001
binlog.000002 ca.pem '#ib_16384_0.dblwr' ibdata1 '#innodb_temp' mysql.sock private_key.pem server-key.pem undo_002
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3cd024c79c7 mysql "docker-entrypoint.s…" 43 seconds ago Up 42 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 56a8c14e1404 3 months ago 603MB
2、进入 mysql 容器操作数据库
[root@localhost ~]# docker exec -it mysql bash
bash-5.1# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 9.1.0 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select version();
+-----------+
| version() |
+-----------+
| 9.1.0 |
+-----------+
1 row in set (0.00 sec)
3、宿主机使用 mysql 工具操作数据库
[root@localhost ~]# dnf install mysql -y
[root@localhost ~]# mysql -u root -p -h 127.0.0.1 -P 3306
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)