当前位置:   article > 正文

docker || 启动mysql容器_docker启动mysql容器

docker启动mysql容器

第1步:拉取mysql镜像。

[root@docker ~]# docker pull mysql:5.7.41

第2步:启动mysql容器。

  1. [root@docker ~]# docker run -d --name sc-mysql-1 -p 33060:3306 -e MYSQL_ROOT_PASSWORD='sc123456' mysql:5.7.41
  2. 5a758962f18df44b0d8c8377b9652bfb791690fa0d15b495f3377ea73dca1463
  3. [root@docker ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 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
  6. [root@docker ~]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. mysql 5.7.41 0018a8d83892 11 hours ago 455MB
  9. [root@docker ~]# netstat -anplut|grep 3306
  10. tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 11118/docker-proxy
  11. tcp6 0 0 :::33060 :::* LISTEN 11123/docker-proxy

docker-proxy 是docker底层帮助宿主机和容器之间网络通信的,做端口映射。

第3步:进入容器内部;在容器内部登录mysql服务。

  1. [root@docker ~]# docker exec -it sc-mysql-1 bash
  2. bash-4.2# ls
  3. bin dev entrypoint.sh home lib64 mnt proc run srv tmp var
  4. boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
  5. bash-4.2# cat /etc/issue
  6. \S
  7. Kernel \r on an \m
  8. bash-4.2# mysql -uroot -p'sc123456'
  9. mysql: [Warning] Using a password on the command line interface can be insecure.
  10. Welcome to the MySQL monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 2
  12. Server version: 5.7.41 MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  14. Oracle is a registered trademark of Oracle Corporation and/or its
  15. affiliates. Other names may be trademarks of their respective
  16. owners.
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  18. mysql> show databases;
  19. +--------------------+
  20. | Database |
  21. +--------------------+
  22. | information_schema |
  23. | mysql |
  24. | performance_schema |
  25. | sys |
  26. +--------------------+
  27. 4 rows in set (0.00 sec)
  28. mysql> create database sanchuang;
  29. Query OK, 1 row affected (0.00 sec)
  30. mysql> show databases;
  31. +--------------------+
  32. | Database |
  33. +--------------------+
  34. | information_schema |
  35. | mysql |
  36. | performance_schema |
  37. | sanchuang |
  38. | sys |
  39. +--------------------+
  40. 5 rows in set (0.01 sec)
  41. mysql> show tables;
  42. +---------------------+
  43. | Tables_in_sanchuang |
  44. +---------------------+
  45. | t1 |
  46. +---------------------+
  47. 1 row in set (0.00 sec)
  48. mysql> exit
  49. 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
  1. [root@MySQL ~]# mysql -h 192.168.102.136 -P33060 -uroot -p'sc123456'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 7
  5. Server version: 5.7.41 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. root@(none) 16:18 mysql>use mysql
  12. Reading table information for completion of table and column names
  13. You can turn off this feature to get a quicker startup with -A
  14. Database changed
  15. root@mysql 16:19 mysql>select user,host from user;
  16. +---------------+-----------+
  17. | user | host |
  18. +---------------+-----------+
  19. | root | % |
  20. | mysql.session | localhost |
  21. | mysql.sys | localhost |
  22. | root | localhost |
  23. +---------------+-----------+
  24. 4 rows in set (0.00 sec)
  25. root@mysql 16:19 mysql>create user 'wjl'@'%'identified by 'sc123456';
  26. Query OK, 0 rows affected (0.00 sec)
  27. root@mysql 16:20 mysql>grant all on *.* to 'wjl'@'%';
  28. Query OK, 0 rows affected (0.00 sec)
  29. root@mysql 16:20 mysql>
grant 是mysql里的授权的命令
all 所有的权限: select insert update delete等
on *.* 在所有的库里的所有的表都可以操作 第一个*表示库 第2个*表示表
  1. MySQL [mysql]> create database sanchuang;
  2. Query OK, 1 row affected (0.00 sec)
  3. MySQL [mysql]> use sanchuang
  4. Database changed
  5. MySQL [sanchuang]> create table t1(id int);
  6. Query OK, 0 rows affected (0.02 sec)

进入容器内部进行查看文件夹和文件

exec 执行
-it 开启一个交互式的终端,进行操作
interaction terminal
sc-mysql-1 容器的名字
bash 进入容器里面执行的程序
  1. [root@docker ~]# docker exec -it sc-mysql-1 bash
  2. bash-4.2# cd /var/lib/mysql
  3. bash-4.2# ls
  4. auto.cnf client-cert.pem ib_logfile0 ibtmp1 performance_schema sanchuang sys
  5. ca-key.pem client-key.pem ib_logfile1 mysql private_key.pem server-cert.pem
  6. ca.pem ib_buffer_pool ibdata1 mysql.sock public_key.pem server-key.pem
  7. bash-4.2# ls sanchuang/
  8. db.opt t1.frm t1.ibd
  9. bash-4.2#

在Ubuntu的机器上远程登录容器的mysql

  1. wei@docker:~$ sudo mysql -h 192.168.102.136 -P33060 -uroot -p'sc123456'
  2. [sudo] password for wei:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12
  5. Server version: 5.7.41 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MySQL [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | information_schema |
  13. | mysql |
  14. | performance_schema |
  15. | sanchuang |
  16. | sys |
  17. +--------------------+
  18. 5 rows in set (0.003 sec)
  19. MySQL [(none)]>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/710253
推荐阅读
相关标签
  

闽ICP备14008679号