简体   繁体   English

使用自定义端口连接数据库 Codeigniter 4

[英]Connecting Database Codeigniter 4 With Custom Ports

So, I'm trying to connect to my database container from my webserver container.所以,我正在尝试从我的网络服务器容器连接到我的数据库容器。 I currently use Codeignter 4 for my PHP framework.我目前将 Codeignter 4 用于我的 PHP 框架。 Everything goes well in terms of communicating between different containers because that container is inside the same networks.就不同容器之间的通信而言,一切都很顺利,因为该容器位于相同的网络中。 Inside webserver container, I've tried to ping from and to database container with no problem, All the port is accessible because I can connect PHPMyAdmin which in their own snuggle little container and connect to my DB container with no problem.在 webserver 容器中,我尝试从数据库容器 ping 到数据库容器没有问题,所有端口都可以访问,因为我可以连接 PHPMyAdmin ,它在他们自己的依偎小容器中并毫无问题地连接到我的数据库容器。

This is the backtrace这是回溯

SYSTEMPATH/Database/BaseConnection.php : 618   —  CodeIgniter\Database\BaseConnection->initialize ()

I tried to DD from my controller hoping to override any return view我试图从我的 controller DD 希望覆盖任何返回视图

$db = \Config\Database::connect();
$apakek = $db->query("SELECT * FROM student_details_dummy");
dd($apakek);

This is my DB Array inside App\Database.php这是我在 App\Database.php 中的数据库数组

public $default = [
    'DSN'      => '',
    'hostname' => '172.21.0.4',
    'username' => 'kr_rw',
    'password' => 'MrSLwwZvwC1KCRm6',
    'database' => 'kr_main',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'development'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 21236,
];

I've tried using hostname db_mysql Which is pingable with the corresponding port to even check open port on canyouseeme.org returning fine.我试过使用主机名 db_mysql ,它可以用相应的端口 ping 甚至检查 canyouseeme.org 上的开放端口返回正常。

How should I resolve this?我应该如何解决这个问题?

PS: all necessary extension is installed and enabled including php_mysqli php_mbstring php_pdo should any miss please do point out. PS:所有必要的扩展都已安装并启用,包括 php_mysqli php_mbstring php_pdo 如果有任何遗漏请指出。

If it's on docker you have to open the port outside and the host should be your container name如果它在 docker 上,则必须在外部打开端口,并且主机应该是您的容器名称

ports:
  - "33088:3306"

and

'hostname' => 'container_name',

If you're not using .env to pass config params to docker, then specify it on container level.如果您不使用.env将配置参数传递给 docker,请在容器级别指定它。 (example below) (下面的例子)

environment:
  DB_DATABASE: db_mysql
  DB_USERNAME: .........

Example code of my project我的项目的示例代码

In docker-compose.yml在 docker-compose.yml

services:
  ## -----------------------------------------------
  ##           MySql database
  ## -----------------------------------------------
  db_mysql:
    image: mysql:5.7
    restart: always
    volumes:
      - db_mysql:/var/lib/mysql
      - ./mysql:/docker-entrypoint-initdb.d
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
    deploy:
      mode: global
    ports:
      - "33088:3306"

In Database file (I used Laravel. But in Codeigniter, it's the same)在数据库文件中(我使用了 Laravel。但在 Codeigniter 中,它是一样的)

public $default = [
    'DSN'      => '',
    'hostname' => 'db_mysql',
    'username' => 'root',
    'password' => '',
    'database' => 'kr_main',
    'port'     => 33088,
];

So, I'm myself not sure why this is the case.所以,我自己不确定为什么会这样。 The network I created for those 3 containers acted like local network for a bunch of computer.我为这 3 个容器创建的网络就像一堆计算机的本地网络。 So its basically boil downs to this:所以它基本上归结为:

  1. Outside the Docker Network: Use the Port Forwarding. Docker 网络外:使用端口转发。
  2. Inside the Docker Network: Use the default port of the container. Docker 网络内部:使用容器的默认端口。

So in the end I just use 3306 or the default port for accessing mysql.所以最后我只使用3306或默认端口来访问mysql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM