1

EDIT: Connecting on the commandline with this command mysql -u root -proot -h 127.0.0.1 did not work. Connecting without the -h option works fine. So it has to be an issue with the mariadb config. I added the config at the end.

I have a really weird issue since changing my vagrant environment to Alpine. I cannot connect from a simple php script to the MariaDB server running on the server. Connecting from the mysql commandline works without any issue, even with the same users.

Here is the PHP script I use:

<?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();
}

The error printed is: SQLSTATE[HY000] [2002] Connection refused

The Ansible script to setup the database looks as follows:

---
- 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

Can someone help me with this?

EDIT: I should add, the php script is executed within the vagrant vm.

My mariadb config:

#
# 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]
Knerd
  • 1,892
  • 3
  • 28
  • 55
  • 1
    Is MySQL running in vagrant and the PHP script as well? Can you connect via the command line *from within vagrant?* – Evert Feb 25 '19 at 20:20
  • Yes, both are running in the vagrant environment. – Knerd Feb 25 '19 at 20:35
  • @Knerd You said that connecting to mysql via the command line works, but also that connecting to mysql via the command line does NOT work. So what is it? And what command are you using when it is working and not working? – Progman Feb 25 '19 at 20:53
  • @Progman Connecting on the commandline with this command `mysql -u root -proot -h 127.0.0.1` did not work. Connecting without the -h option works fine. I think it has something to do with the connection via TCP/IP but I cannot find anything with google. – Knerd Feb 25 '19 at 20:54
  • @Progman I have a question. Before switching to Alpine I used mysql on Ubuntu. Does mysql turn skip-networking off by default? – Knerd Feb 25 '19 at 21:13

1 Answers1

-1

Have you tried the solution described there ? Getting Error SQLSTATE[HY000] [2002] Connection refused on NAS Synology

to specify the port in the same field as the host with the ":".

Tuckbros
  • 417
  • 3
  • 13