暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mysql docker查看容器地址及连接参考

原创 jieguo 2023-05-10
1390

查看容器的IP地址:
[root@test ~]# docker inspect -f ‘{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ $(docker ps -aq)
/mysql - 192.168.111.2
[root@test ~]# docker inspect -f ‘{{.Name}} - {{.NetworkSettings.IPAddress }}’ $(docker ps -aq)
/mysql -
[root@test ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81306c8cf731 mysql:8.0 "docker-entrypoint.s鈥 7 weeks ago Up 22 hours 33060/tcp, 0.0.0.0:13389->3306/tcp mysql
[root@test ~]# docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ 81306c8cf731
192.168.111.2
[root@test ~]# docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ mysql
192.168.111.2
[root@test ~]# docker inspect --format=’{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ $(docker ps -aq)
/mysql - 192.168.111.2

[root@test ~]# docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
/mysql - 192.168.111.2
[root@test ~]# docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
/mysql - 
[root@test ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS        PORTS                               NAMES
81306c8cf731   mysql:8.0   "docker-entrypoint.s鈥   7 weeks ago   Up 22 hours   33060/tcp, 0.0.0.0:13389->3306/tcp   mysql
[root@test ~]# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 81306c8cf731
192.168.111.2
[root@test ~]# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql
192.168.111.2
[root@test ~]# docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
/mysql - 192.168.111.2
[root@test ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS        PORTS                               NAMES
81306c8cf731   mysql:8.0   "docker-entrypoint.s鈥   7 weeks ago   Up 22 hours   33060/tcp, 0.0.0.0:13389->3306/tcp   mysql
[root@test ~]# 

[root@test data]# ls
data  docker-compose-8.yml  docker-compose.yml  my.cnf  mysql  mysql8
[root@test data]# more docker-compose-8.yml 
version: '3'
services:
  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      - "MYSQL_ROOT_PASSWORD=password"
      - "TZ=Asia/Shanghai"
    volumes:
      - /data/my.cnf:/etc/my.cnf
      - /data/mysql8/conf:/etc/mysql/conf.d
      - /data/mysql8/logs:/logs
      - /data/mysql8/data:/var/lib/mysql
    command:
        --lower_case_table_names=1
        --character-set-server=utf8mb4
        --collation-server=utf8mb4_general_ci
        --init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_general_ci'
    ports:
      - 13389:3306

[root@test data]# more /data/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

max_heap_table_size=256M
tmp_table_size=256M

[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
[root@test data]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@test data]# pwd
/data
[root@test data]# ps -ef|grep sock
root      9713     1  0 May09 ?        00:00:20 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root     10207     1  0 May09 ?        00:00:05 /usr/bin/containerd-shim-runc-v2 -namespace moby -id 81306c8cf731078bbde83b8c3e8493878b65451ac4ad0fc86a65140407f3b16c -address /run/containerd/containerd.sock
root     19576 16706  0 14:30 pts/4    00:00:00 grep --color=auto sock
[root@test data]# pwd
/data
[root@test data]# cd mysql8/data/
[root@test data]# pwd
/data/mysql8/data
[root@test data]# ll
total 14556748
-rw-r----- 1 polkitd input         56 Nov 18 20:55 auto.cnf
-rw-r----- 1 polkitd input 1074060591 Apr 25 03:14 binlog.000009
-rw-r----- 1 polkitd input 1073794065 Apr 27 03:40 binlog.000010
-rw-r----- 1 polkitd input 1073742757 Apr 29 04:36 binlog.000011
-rw-r----- 1 polkitd input 1073742143 May  1 22:57 binlog.000012
-rw-r----- 1 polkitd input 1073991331 May  4 03:01 binlog.000013
-rw-r----- 1 polkitd input 1073742207 May  8 04:36 binlog.000015
-rw-r----- 1 polkitd input 1073743480 May  8 13:34 binlog.000016
-rw-r----- 1 polkitd input 1132957150 May  8 17:01 binlog.000017
-rw-r----- 1 polkitd input  991692917 May  9 16:20 binlog.000018
-rw-r----- 1 polkitd input 1073742827 May 10 13:59 binlog.000019
-rw-r----- 1 polkitd input  609149761 May 10 14:30 binlog.000020
-rw-r----- 1 polkitd input        272 May 10 13:59 binlog.index
-rw------- 1 polkitd input       1676 Nov 18 20:55 ca-key.pem
-rw-r--r-- 1 polkitd input       1112 Nov 18 20:55 ca.pem
-rw-r--r-- 1 polkitd input       1112 Nov 18 20:55 client-cert.pem
-rw------- 1 polkitd input       1676 Nov 18 20:55 client-key.pem
drwxr-x--- 2 polkitd input       4096 Mar 22 11:32 datahub
drwxr-x--- 2 polkitd input      12288 May 10 13:49 dby
-rw-r----- 1 polkitd input     196608 May 10 14:30 #ib_16384_0.dblwr
-rw-r----- 1 polkitd input    8585216 May 10 14:24 #ib_16384_1.dblwr
-rw-r----- 1 polkitd input      21879 Mar 22 13:30 ib_buffer_pool
-rw-r----- 1 polkitd input   79691776 May 10 14:30 ibdata1
-rw-r----- 1 polkitd input   12582912 May  9 16:19 ibtmp1
drwxr-x--- 2 polkitd input       4096 May 10 14:24 #innodb_redo
drwxr-x--- 2 polkitd input       4096 May  9 16:19 #innodb_temp
drwxr-x--- 2 polkitd input       4096 Nov 18 20:55 mysql
-rw-r----- 1 polkitd input   37748736 May 10 14:24 mysql.ibd
lrwxrwxrwx 1 polkitd input         27 May  9 16:19 mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x--- 2 polkitd input       4096 Nov 18 20:55 performance_schema
-rw------- 1 polkitd input       1680 Nov 18 20:55 private_key.pem
-rw-r--r-- 1 polkitd input        452 Nov 18 20:55 public_key.pem
-rw-r--r-- 1 polkitd input       1112 Nov 18 20:55 server-cert.pem
-rw------- 1 polkitd input       1680 Nov 18 20:55 server-key.pem
drwxr-x--- 2 polkitd input       4096 Nov 18 20:55 sys
drwxr-x--- 2 polkitd input      12288 May 10 13:36 test
-rw-r----- 1 polkitd input  889192448 May 10 14:30 undo_001
-rw-r----- 1 polkitd input  822083584 May 10 14:30 undo_002
[root@test data]# pwd
/data/mysql8/data
[root@test data]# ip a|grep net
    inet 127.0.0.1/8 scope host lo
    inet 10.21.40.8/20 brd 10.21.47.255 scope global dynamic eth0
    inet 10.17.0.1/16 brd 10.17.255.255 scope global docker0
    inet 10.23.0.1/16 brd 10.23.255.255 scope global br-7d2db3009f21
    inet 192.168.111.1/20 brd 192.168.159.255 scope global br-4cff6c18d025
    link/ether 66:63:b8:65:ff:5b brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@test data]# mysql --host=10.21.40.8 --user='root' --password="password" --port=13389 -s /data/mysql8/data/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000): Unknown database '/data/mysql8/data/mysql.sock'
[root@test data]# mysql --host=10.21.40.8 --user='root' --password="password" --port=13389 -S /data/mysql8/data/mysql.sock
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 744
Server version: 8.0.30 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                |
| test               |
+--------------------+
7 rows in set (0.01 sec)

mysql> create database t;
Query OK, 1 row affected (0.05 sec)

mysql> exit
Bye
[root@test data]# mysql --host=192.168.111.2 --user='root' --password="password" --port=3306
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 745
Server version: 8.0.30 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                |
| t                  |
| test               |
+--------------------+
8 rows in set (0.00 sec)

mysql> exit
Bye
[root@test data]# mysql --host=192.168.111.2 --user='root' --password="password" --port=3306
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 746
Server version: 8.0.30 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                |
| t                  |
| test               |
+--------------------+
8 rows in set (0.00 sec)

mysql> drop database t;
Query OK, 0 rows affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
7 rows in set (0.00 sec)

mysql> exit
Bye
[root@test data]# mysql --host=10.21.40.8 --user='root' --password="password" --port=13389 -S /data/mysql8/data/mysql.sock
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 747
Server version: 8.0.30 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                |
| test               |
+--------------------+
7 rows in set (0.00 sec)

mysql> exit

最后修改时间:2023-05-10 15:04:10
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论