Php 无法通过PDO连接,但命令行工作正常

Php 无法通过PDO连接,但命令行工作正常,php,mysql,mariadb,Php,Mysql,Mariadb,编辑:在命令行上使用此命令mysql-u root-proot-h 127.0.0.1连接不起作用。不使用-h选项进行连接可以正常工作。所以这一定是mariadb配置的问题。我在末尾添加了配置 自从把我的流浪环境改成阿尔卑斯山以来,我有一个非常奇怪的问题。我无法从简单的php脚本连接到服务器上运行的MariaDB服务器。从mysql命令行进行连接没有任何问题,即使使用相同的用户 以下是我使用的PHP脚本: <?php $dsn = 'mysql:dbname=mysql;host=127.

编辑:在命令行上使用此命令mysql-u root-proot-h 127.0.0.1连接不起作用。不使用-h选项进行连接可以正常工作。所以这一定是mariadb配置的问题。我在末尾添加了配置

自从把我的流浪环境改成阿尔卑斯山以来,我有一个非常奇怪的问题。我无法从简单的php脚本连接到服务器上运行的MariaDB服务器。从mysql命令行进行连接没有任何问题,即使使用相同的用户

以下是我使用的PHP脚本:

<?php
$dsn = 'mysql:dbname=mysql;host=127.0.0.1;port=3306';
$user = 'root';
$password = 'root';

try {
    $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo $e->getMessage();
}
有人能帮我吗

编辑:我应该补充一点,php脚本是在vagrant vm中执行的

我的mariadb配置:

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see

# this is read by the standalone daemon and embedded servers
[server]
bind-address=0.0.0.0

# this is only for the mysqld standalone daemon
[mysqld]
skip-networking

# Galera-related settings
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.3 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.3]

您是否尝试过上面描述的解决方案?


要在与主机相同的字段中指定端口,请使用:。

MySQL是否也在vagrant和PHP脚本中运行?您能从vagrant中通过命令行连接吗?是的,两者都在vagrant环境中运行。@Knerd您说过通过命令行连接mysql是可行的,但是通过命令行连接mysql是不可行的。那是什么呢?在命令行上使用mysql-u root-proot-h 127.0.0.1连接命令时,您使用的是什么命令。不使用-h选项进行连接可以正常工作。我认为这与通过TCP/IP的连接有关,但我在google上找不到任何东西。@Progman我有一个问题。在切换到Alpine之前,我在Ubuntu上使用了mysql。mysql是否默认关闭skip networking?无论出于何种原因,我都打开了skip networking。
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see

# this is read by the standalone daemon and embedded servers
[server]
bind-address=0.0.0.0

# this is only for the mysqld standalone daemon
[mysqld]
skip-networking

# Galera-related settings
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.3 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.3]