【问题标题】:How to close open connections in mysql with codeigniter如何使用codeigniter关闭mysql中的打开连接
【发布时间】:2018-02-02 11:08:31
【问题描述】:

我只想知道如何关闭来自 mysql 的所有打开的连接,它会提高我网站的速度。

请看一下mysql状态输出

Variable_name     Value
Aborted_connects    1
Connection_errors_accept    0
Connection_errors_internal    0
Connection_errors_max_connections    0
Connection_errors_peer_address    0
Connection_errors_select    0
Connection_errors_tcpwrap    0
Connections    252
Max_used_connections    6
Performance_schema_session_connect_attrs_lost    0
Ssl_client_connects    0
Ssl_connect_renegotiates    0
Ssl_finished_connects    0
Threads_connected    1

我正在为我的项目使用 code-igniter v3,并在 database.php 中使用以下数据库设置

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '#######',
    'database' => '#######',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

【问题讨论】:

  • 您的输出状态仅显示一个Threads_connected。如果您担心Connections 统计信息,请不要担心。这是连接到 MySQL 服务器的尝试次数(成功与否)。换句话说,在 252 次连接尝试中,当前只有一个连接。如果这是手机通话记录,则意味着您接到了 252 个电话,其中一个仍在进行中。

标签: codeigniter mysqli


【解决方案1】:

你不应该做任何事情。

我相信 Codeigniter/MySQL 会为您关闭连接,因为您没有启用持久连接 ('pconnect' => FALSE),这将允许自动关闭连接。

https://codeigniter.com/user_guide/database/connecting.html#manually-closing-the-connection

虽然 CodeIgniter 会智能地关闭您的数据库连接,但您可以显式关闭连接。

$this->db->close();

【讨论】:

  • 连接正在增加
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多