如何让 PHP 使用 UNIX 套接字连接数据库(Joomla)

如何让 PHP 使用 UNIX 套接字连接数据库(Joomla)

我希望我的 PHP 应用程序(例如 Joomla)使用 UNIX 套接字连接到本地 mysql 数据库。

我已经配置了 php.ini:

mysqli.default_socket = /var/run/mysqld/mysqld.sock
mysql.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

但在 Joomla 中,我只能将 localhost 或 127.0.0.1 设置为数据库,是否可以覆盖此功能?如何测试 UNIX 套接字是否实际被使用?

我的 MySQL/MariaDB 配置如下:

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

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
skip-external-locking
bind-address        = 127.0.0.1
max_connections     = 500
connect_timeout     = 10
wait_timeout        = 600
max_allowed_packet  = 16M
thread_cache_size       = 1000
sort_buffer_size    = 512M
bulk_insert_buffer_size = 16M
tmp_table_size      = 8G
max_heap_table_size = 8G

myisam_recover          = FORCE,BACKUP
key_buffer_size     = 128M
open-files-limit    = 65535
table_open_cache    = 10240
table_open_cache_instances = 8
table-definition-cache = 4096
myisam_sort_buffer_size = 512M
key-cache-segments=8
concurrent_insert   = 2
read_buffer_size    = 32M
read_rnd_buffer_size    = 64M

query_cache_limit       = 96M
query_cache_size        = 128M
query_cache_min_res_unit=7108
query_cache_type        = 1

log_warnings        = 2

slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
log_slow_verbosity  = query_plan


log_bin         = /var/log/mysql/mariadb-bin
log_bin_index       = /var/log/mysql/mariadb-bin.index
sync_binlog     = 1
expire_logs_days    = 14
max_binlog_size         = 100M

default_storage_engine  = InnoDB
innodb_log_file_size    = 50M
innodb_buffer_pool_size = 6G
innodb_buffer_pool_instances=8
innodb_log_buffer_size  = 32M
innodb_file_per_table   = 1
innodb_concurrency_tickets=5000
innodb_open_files   = 240000
innodb_io_capacity  = 240000
innodb_flush_method = O_DIRECT
innodb-log-files-in-group      = 2
innodb-flush-log-at-trx-commit = 1

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 512M

!includedir /etc/mysql/conf.d/

答案1

127.0.0.1 用于 TCP 套接字。

'localhost' 用于 Unix 文件系统套接字。

我们可以用来netstat -ln | grep 'mysql'确定连接方法,并探索选项来强制执行特定类型

shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP
shell> mysql --host=localhost
shell> mysql --host=localhost --protocol=TCP

连接参数应该是这样的

$link = mysql_connect('localhost:/var/run/mysqld/mysqld.sock', 'mysql_user', 'mysql_password');

var $host = ':/var/run/mysqld/mysqld.sock';
var $user = 'your_user_db_name';
var $db = 'your_db_name';
var $password = 'your_db_password';

答案2

在 libraries/joomla/database/database.php 中定义了抽象类 JDatabase,然后在 libraries/joomla/database/driver/ 中为不同数据库类型实现该类,在本例中为 mysql 或 mysqli..

在 mysqli.php (Joomla! 3.3.6) 中,如果 configuration.php 中未指定任何内容,则 __construct($options) 将设置默认值,将其更改为 'localhost'。从第 64 行开始:

public function __construct($options)
{
// Get some basic values from the options.
$options['host']     = (isset($options['host'])) ? $options['host'] : 'localhost';
$options['user']     = (isset($options['user'])) ? $options['user'] : 'root';
$options['password'] = (isset($options['password'])) ? $options['password'] : '';
$options['database'] = (isset($options['database'])) ? $options['database'] : '';
$options['select']   = (isset($options['select'])) ? (bool) $options['select'] : true;
$options['port']     = null;
$options['socket']   = null;

// Finalize initialisation.
parent::__construct($options);
}

然后,函数 connect() 最终使用它们在 163-165 行创建连接:

    $this->connection = @mysqli_connect(
        $this->options['host'], $this->options['user'], $this->options['password'],
        null, $this->options['port'], $this->options['socket']
    );

如果没有其他规定,mysqli_connect()使用 php.ini 中的默认主机名、端口和套接字

mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") 
   [, string $username = ini_get("mysqli.default_user") 
   [, string $passwd = ini_get("mysqli.default_pw") 
   [, string $dbname = "" 
   [, int $port = ini_get("mysqli.default_port") 
   [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

现在您知道了影响这些连接参数的整个轨迹。通过编辑 mysqli.php,您应该能够强制执行 configuration.php 不允许您更改的设置。但是,您应该小心并考虑记录这些修改,因为

  1. configuration.php 不再像文档中描述的那样工作
  2. Joomla 更新可以(并最终将)覆盖您的修改
  3. 其他因素可能取决于此特定配置

相关内容