在 PHP 中设置默认 MySQL 套接字,pdo_mysql.default_socket PHP 5.3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14205826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Default MySQL Socket in PHP, pdo_mysql.default_socket PHP 5.3
提问by Conner Stephen McCabe
I'm having an issue setting the default PDO socket location for MySQL, without doing it through the initialisation of the PDO class into a variable. I've tried altering the mysql.default_socket variable. But no joy with that. The only way that works currently is through the class DSN instantiation.
我在为 MySQL 设置默认 PDO 套接字位置时遇到问题,而没有通过将 PDO 类初始化为变量来完成。我试过改变 mysql.default_socket 变量。但这并不快乐。当前唯一有效的方法是通过类 DSN 实例化。
php.ini Contents:
php.ini 内容:
[PHP]
engine = On
zend.ze1_compatibility_mode = Off
short_open_tag = On
asp_tags = Off
precision = 14
y2k_compliance = On
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func=
serialize_precision = 100
allow_call_time_pass_reference = Off
safe_mode = Off
safe_mode_gid = Off
safe_mode_include_dir =
safe_mode_exec_dir =
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
disable_functions =
disable_classes =
expose_php = Off
max_execution_time = 300
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
variables_order = "EGPCS"
register_globals = Off
register_long_arrays = Off
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
extension_dir = "/usr/lib64/php/modules"
enable_dl = On
file_uploads = On
upload_max_filesize = 500M
allow_url_fopen = On
default_socket_timeout = 60
[Syslog]
define_syslog_variables = Off
sendmail_path = /usr/sbin/sendmail -t -i
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQL]
mysql.allow_persistent = On
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port = 3306
mysql.default_socket = /home/mysql/mysql.sock
mysql.default_host = 127.0.0.1
mysql.default_user = conner
mysql.default_password = danica4eva
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket = /home/mysql/mysql.sock
mysqli.default_host = 127.0.0.1
mysqli.default_user = conner
mysqli.default_pw = danica4eva
mysqli.reconnect = Off
[mSQL]
msql.allow_persistent = On
msql.max_persistent = -1
msql.max_links = -1
[PostgresSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase]
sybase.allow_persistent = On
sybase.max_persistent = -1
sybase.max_links = -1
sybase.min_error_severity = 10
sybase.min_message_severity = 10
sybase.compatability_mode = Off
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[Informix]
ifx.default_host =
ifx.default_user =
ifx.default_password =
ifx.allow_persistent = On
ifx.max_persistent = -1
ifx.max_links = -1
ifx.textasvarchar = 0
ifx.byteasvarchar = 0
ifx.charasvarchar = 0
ifx.blobinfile = 0.
ifx.nullformat = 0
[Session]
session.save_handler = files
session.save_path = "/var/lib/php/session"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = 0
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 1
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off
[Verisign Payflow Pro]
pfpro.defaulthost = "test-payflow.verisign.com"
pfpro.defaultport = 443
pfpro.defaulttimeout = 30
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
zend_extension = /etc/ioncube/ioncube_loader_lin_5.3.so
As you can see, I have changed the default socket settings within the php.ini file, but I still get a socket error in PDO when I try to connect to MySQL. It leads me to believe that the PDO driver is not using the settings from the php.ini. I was sure that there was some way of configuring PDO, but there was nothing within my php.ini. Should there be settings for PDO within the php.ini, or are they contained elsewhere?
如您所见,我已经更改了 php.ini 文件中的默认套接字设置,但是当我尝试连接到 MySQL 时,在 PDO 中仍然出现套接字错误。这让我相信 PDO 驱动程序没有使用 php.ini 中的设置。我确信有某种方式可以配置 PDO,但我的 php.ini 中没有任何内容。php.ini 中是否应该有 PDO 的设置,或者它们是否包含在其他地方?
回答by hek2mgl
There is a different ini setting for the PDO mysql driver. It is called
PDO mysql 驱动程序有不同的 ini 设置。它被称为
pdo_mysql.default_socket
Have you tried to set this ini value? Please refer to http://php.net/manual/de/ref.pdo-mysql.php
您是否尝试过设置此 ini 值?请参考http://php.net/manual/de/ref.pdo-mysql.php
The value can also being influenced during compile time using the following configure option:
使用以下配置选项也可以在编译期间影响该值:
--with-mysql-sock=/my/path/mysql.sock
Update: I've figured out that the problem is a known PHP bug.
更新:我发现问题是一个已知的PHP 错误。
First: you can workaround the problem when using 127.0.0.1 as the db host instead of localhost. PHP will connect trough a TCP socket this way.
第一:当使用 127.0.0.1 作为数据库主机而不是 localhost 时,您可以解决这个问题。PHP 将通过这种方式通过 TCP 套接字进行连接。
To change the location you'll have to compile PHP from sources with special configuration options. I was successful to change the location of the mysql socket with the following (basic) command lines :
要更改位置,您必须从具有特殊配置选项的源代码编译 PHP。我成功地使用以下(基本)命令行更改了 mysql 套接字的位置:
./configure \
--with-mysql \
--with-mysqli=mysqlnd \
--enable-pdo \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/home/mysql/mysqld.sock
make
make install
The final trick made the combination of the last both of them. --with-mysql-sock=filesets the location of the .sock file butit is just used if --with-pdo-mysql=is set to mysqlnd(mysql native driver). If this option is missing pdo would be linked against the libmysqlclient from the system. As a result of this you can also recompile libmysqlclient (with modified socket path) and leave php untouched.
最后一招使最后两者结合。--with-mysql-sock=file设置 .sock 文件的位置,但仅在--with-pdo-mysql=设置为mysqlnd(mysql native driver) 时使用。如果缺少此选项,则 pdo 将链接到系统中的 libmysqlclient。因此,您还可以重新编译 libmysqlclient(使用修改后的套接字路径)并保持 php 不变。
Note: This is just the basic configuration to make PHP working with PDO. You'll need additional configuration options if you need additional extensions (like json, xml, whatever). You'll find the configuration that your Linux Distribution has used to compile PHP if you execute the (original distribution) version this way: php -i.
注意:这只是使 PHP 与 PDO 一起工作的基本配置。如果你需要额外的扩展(比如 json、xml 等等),你将需要额外的配置选项。你会发现配置你的Linux发行版已经用于编译PHP如果执行(原分布)的版本是这样的:php -i。

![php 致命错误:未捕获的 SoapFault 异常:[soap:Client]](/res/img/loading.gif)