赞
踩
第1步:拉取mysql镜像。
[root@docker ~]# docker pull mysql:5.7.41
第2步:启动mysql容器。
- [root@docker ~]# docker run -d --name sc-mysql-1 -p 33060:3306 -e MYSQL_ROOT_PASSWORD='sc123456' mysql:5.7.41
- 5a758962f18df44b0d8c8377b9652bfb791690fa0d15b495f3377ea73dca1463
-
- [root@docker ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 5a758962f18d mysql:5.7.41 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp sc-mysql-1
-
- [root@docker ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- mysql 5.7.41 0018a8d83892 11 hours ago 455MB
-
- [root@docker ~]# netstat -anplut|grep 3306
- tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 11118/docker-proxy
- tcp6 0 0 :::33060 :::* LISTEN 11123/docker-proxy
docker-proxy 是docker底层帮助宿主机和容器之间网络通信的,做端口映射。
第3步:进入容器内部;在容器内部登录mysql服务。
- [root@docker ~]# docker exec -it sc-mysql-1 bash
- bash-4.2# ls
- bin dev entrypoint.sh home lib64 mnt proc run srv tmp var
- boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
- bash-4.2# cat /etc/issue
- \S
- Kernel \r on an \m
- bash-4.2# mysql -uroot -p'sc123456'
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, 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> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.00 sec)
-
- mysql> create database sanchuang;
- Query OK, 1 row affected (0.00 sec)
-
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sanchuang |
- | sys |
- +--------------------+
- 5 rows in set (0.01 sec)
-
- mysql> show tables;
- +---------------------+
- | Tables_in_sanchuang |
- +---------------------+
- | t1 |
- +---------------------+
- 1 row in set (0.00 sec)
-
- mysql> exit
- Bye
在宿主机上安装mariadb和mariadb-server
[root@docker ~]# yum install mariadb mariadb-server -y
mysql -h 192.168.102.136 -P33060 -uroot -p'sc123456'
-h 192.168.223.131 指定远程连接的mysql服务器的ip地址 host
-P 指定端口 port
-p 指定密码 password
-u 指定用户名 user
- [root@MySQL ~]# mysql -h 192.168.102.136 -P33060 -uroot -p'sc123456'
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 7
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2022, 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.
-
- root@(none) 16:18 mysql>use mysql
- 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
- root@mysql 16:19 mysql>select user,host from user;
- +---------------+-----------+
- | user | host |
- +---------------+-----------+
- | root | % |
- | mysql.session | localhost |
- | mysql.sys | localhost |
- | root | localhost |
- +---------------+-----------+
- 4 rows in set (0.00 sec)
-
- root@mysql 16:19 mysql>create user 'wjl'@'%'identified by 'sc123456';
- Query OK, 0 rows affected (0.00 sec)
-
- root@mysql 16:20 mysql>grant all on *.* to 'wjl'@'%';
- Query OK, 0 rows affected (0.00 sec)
-
- root@mysql 16:20 mysql>
grant 是mysql里的授权的命令
all 所有的权限: select insert update delete等
on *.* 在所有的库里的所有的表都可以操作 第一个*表示库 第2个*表示表
- MySQL [mysql]> create database sanchuang;
- Query OK, 1 row affected (0.00 sec)
-
- MySQL [mysql]> use sanchuang
- Database changed
- MySQL [sanchuang]> create table t1(id int);
- Query OK, 0 rows affected (0.02 sec)
进入容器内部进行查看文件夹和文件
exec 执行
-it 开启一个交互式的终端,进行操作
interaction terminal
sc-mysql-1 容器的名字
bash 进入容器里面执行的程序
- [root@docker ~]# docker exec -it sc-mysql-1 bash
- bash-4.2# cd /var/lib/mysql
- bash-4.2# ls
- auto.cnf client-cert.pem ib_logfile0 ibtmp1 performance_schema sanchuang sys
- ca-key.pem client-key.pem ib_logfile1 mysql private_key.pem server-cert.pem
- ca.pem ib_buffer_pool ibdata1 mysql.sock public_key.pem server-key.pem
- bash-4.2# ls sanchuang/
- db.opt t1.frm t1.ibd
- bash-4.2#
在Ubuntu的机器上远程登录容器的mysql
- wei@docker:~$ sudo mysql -h 192.168.102.136 -P33060 -uroot -p'sc123456'
- [sudo] password for wei:
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MySQL connection id is 12
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- MySQL [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sanchuang |
- | sys |
- +--------------------+
- 5 rows in set (0.003 sec)
-
- MySQL [(none)]>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。