无法通过PDO连接,但命令行正常运行

时间:2019-02-25 20:16:32

标签: php mysql mariadb

编辑:在命令行上使用此命令mysql -u root -proot -h 127.0.0.1的连接无效。没有-h选项的连接可以正常工作。因此,mariadb配置必须是一个问题。我在最后添加了配置。

自从将无所事事的环境更改为Alpine以来,我遇到了一个非常奇怪的问题。我无法从简单的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();
}

打印的错误是:SQLSTATE[HY000] [2002] Connection refused

用于设置数据库的Ansible脚本如下:

---
- name: Install packages
  apk:
    name:
      - mysql
      - mysql-client
      - py3-mysqlclient
  become: true
- name: Prepare mysql
  shell: mysql_install_db --user=mysql --datadir=/var/lib/mysql && /etc/init.d/mariadb setup
  args:
    creates: ~/mysql_install.lock
  become: true
- name: Start mysql
  service:
    name: mariadb
    state: started
  become: true
- name: Set root password
  shell: mysqladmin -u {{mysql_root_user}} password {{mysql_root_password}}
  become: true
- name: Copy config
  copy:
    dest: /etc/my.cnf.d/maria-server-jinya.cnf
    src: /vagrant/vagrant-files/roles/mysql/files/maria-server-jinya.cnf
  become: true
- name: Create database jinya-gallery-cms
  mysql_db:
    name: jinya-gallery-cms
    state: present
    login_user: "{{mysql_root_user}}"
    login_password: "{{mysql_root_password}}"
- name:
  mysql_user:
    name: "{{mysql_jinya_user}}"
    password: "{{mysql_jinya_password}}"
    priv: '*.*:ALL,GRANT'
    state: present
    login_user: "{{mysql_root_user}}"
    login_password: "{{mysql_root_password}}"
  become: true
- name: Import database jinya-gallery-cms
  mysql_db:
    name: jinya-gallery-cms
    state: import
    login_user: "{{mysql_root_user}}"
    login_password: "{{mysql_root_password}}"
    target: /vagrant/vagrant-files/jinya-gallery-cms.sql
- name: Restart mysql
  service:
    name: mariadb
    state: restarted
  become: true

有人可以帮我吗?

编辑:我应该补充,在无业游民的vm中执行php脚本。

我的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]

1 个答案:

答案 0 :(得分:0)

您是否尝试过那里描述的解决方案? Getting Error SQLSTATE[HY000] [2002] Connection refused on NAS Synology

在与主机相同的字段中使用“:”指定端口。