Symfony - How to connect to remote mysql database without install mysql-server - php

I am trying to connect to a remote mysql database with my Symfony application. I don't want to install the mysql-server package on my server if I don't need to but when I try to connect, I get the following error:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] No such file or directory
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
Here's what my database configuration look like. In the variables, I setup pdo_mysql as the driver and the other fields are working well.
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database.driver%
host: %database.host%
port: %database.port%
dbname: %database.name%
user: %database.user%
password: %database.password%
charset: UTF8
Edit1: If I install mysql-server, it works and the error is gone.
Edit2: My server is running Ubuntu 16.04.2 LTS

You should be able to install client on Ubuntu like this:
apt-get install mysql-client
I think 5.5 is the latest.
Go here for setting up MariaDB:
https://downloads.mariadb.org/mariadb/repositories/#mirror=syringa&distro=Ubuntu
makes more sense nowadays to use MariaDB instead:
EDIT #2
to add remote useron the remote system:
CREATE USER 'fooUser'#'192.168.40.200' IDENTIFIED BY 'myPassword';
check the user table to see if they can connect from remote system:
use mysql;
SELECT Host,User,Password,ssl_type from user;
ssl_type is if you are using SSL to connect, you could leave that out.

Related

MySQL8 connection with php7.3 and Symfony 5 does not work

I have a connection problem with a mysql8 database, php7.3 and Symfony 5. The user and the password are correct !
The connection to MySQL8 database must be done from an SSH tunnel
The SSH tunnel is working :
ssh -p [remote_machine_port] -N -L [mysql_tunnel_port]: 127.0.0.1: 3306 -i / [directory] / private_key [username] # [remote_machine_ip]
MySQL access configuration:
GRANT ALL PRIVILEGES ON 'databasename'.* TO '[user]'#'%' WITH GRANT OPTION;
Doctrine configuration :
DATABASE_URL='mysql://[user]:[password]#127.0.0.1:[port_tunnel_mysql]/[databasename]'
An exception occurred in driver: SQLSTATE [HY000] [1130] Host '127.0.0.1' is not allowed to connect to this MySQL server
I have the same problem with heidiSQL and on the VM
I tried with the Ip of the VM instead of 127.0.0.1
ssh -p [port_machine_distant] -N -L [port_tunnel_mysql]:[ip_machine_vm]:3306 -i /[directory]/private_key [username]#[ip_machine_distant]
Doctrine configuration
DATABASE_URL='mysql://[user]:[password]#[ip_machine_vm]:[port_tunnel_mysql]/[databasename]'
An exception occurred in driver: SQLSTATE [HY000] [2002] Connection refused
However, this setting works with heidiSQL but not with the Symfony application !
Did I forget a setting on the VM or Mysql ?
127.0.0.1 connection issue :
We have created the '[user]'#'127.0.0.1' rights.
In MySQL, 127.0.0.1 uses the network and localhost uses the socket.
The message "The server requested authentication method unknown to the client [caching_sha2_password]" was caused because the setting 'default-authentication-plugin=mysql_native_password' was created after the creation of the rights.
We regenerated the rights, and now everything is working fine.

Could not drop database `symfony-project` for connection named default An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

I accidentally created a database symfony-project successfully, but it's not appearing on my phpmyadmin page. I want to drop the database with
php bin/console doctrine:database:drop --force
then this message comes out
Could not drop database symfony-project for connection named default
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
When I try to create a new database, with php bin/console doctrine:database:create, an error message comes out:
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] Connection refused
Because the connection is refused, I can't create and drop any databases right now.
My database url on the .env file is
DATABASE_URL=mysql://root:#127.0.0.1:3306/symfony-project?serverVersion=5.7
My password is empty.
I've tried to change the 127.0.0.1 to localhost and the error message is telling me that
there is no such file or directory.
I'm using symfony 4 and xampp on macOS
UPDATE
I've edited my config/packages/doctrine.yaml file. I commented the url: '%env(resolve:DATABASE_URL)%' and added this:
doctrine:
dbal:
dbname: symfony-project
charset: utf8
host: 127.0.0.1
port: 8080
user: root
password:
driver: pdo_mysql
and now this message comes out
In AbstractMySQLDriver.php line 106:
An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away
In PDOConnection.php line 31:
SQLSTATE[HY000] [2006] MySQL server has gone away
In PDOConnection.php line 27:
SQLSTATE[HY000] [2006] MySQL server has gone away
In PDOConnection.php line 27:
PDO::__construct(): MySQL server has gone away
I think you can also try the connection by attempt to create a database:
php bin/console doctrine:database:create
If you're using Symfony 2, you need to use:
php app/console doctrine:database:create
I also see that in the string:
DATABASE_URL=mysql://root:#127.0.0.1:3306/symfony-project?serverVersion=5.7
You didn't specify the password for root user. Although that might be not an issue if the password is unspecified at the beginning, it's better to re-check these credentials.
You have to configure your doctrine.yaml
doctrine:
dbal:
dbname: database
charset: utf8
user: user
password: secret
driver: pdo_mysql
Check the documentation : https://symfony.com/doc/current/reference/configuration/doctrine.html

Doctrine won't connect to MAMP mysql database

EDIT: I have since resolved my issue thanks to this answer https://stackoverflow.com/a/50908912/12296041
I have started learning Symfony for a school project, and I was following a tutorial from Symfony's website, but for some reason, Doctrine doesn't manage to connect to MySQL database I have running on my computer. I'm on a Macbook, using MAMP to run a local MySQL server.
Whenever I try to execute any doctrine commands that interact with the database such as php bin/console doctrine:database:create it never works.
So far, I have checked that I could indeed connect to the database using PHPMyAdmin. I have also tried to change the DATABASE_URL in the .env file, but this hasn't solved my issue.
I have also tried creating a symbolic link with sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock but that didn't work either.
This is what my .env file looks like:
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# For a PostgreSQL database, use: "postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=11"
# IMPORTANT: You MUST also configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:root#127.0.0.1:8888/db
###< doctrine/doctrine-bundle ###
And I get this error when trying to create a database with doctrine:
In AbstractMySQLDriver.php line 93:
An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] No such file or directory
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] No such file or directory
Anything that can point me in the right direction is greatly appreciated, thanks!
I agree with Cid, port is 8889 by default for MySQL in MAMP.
You can also try to replace 127.0.0.1 by localhost.
Let me know if this works.

Can't create database with Symfony 4.1 : could not find driver

When i do the command php bin/console doctrine:database:create i have these errors :
In AbstractMySQLDriver.php line 126: An exception occurred in
driver: could not find driver
In PDOConnection.php line 50: could not find driver
In PDOConnection.php line 46: could not find driver
config/packages/doctrine.yaml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
.env
DATABASE_URL=mysql://root:root#127.0.0.1:3306/blogMyProject
My PHP Extensions:
php.ini extensions:
PHP 7.2.7 - MySQL 5.7.11 - Symfony 4.1.4 - I use UwAmp 3.1.0 - I'm on Windows
I don't understand where these errors come from.
This works for my case
symfony 4.2
php 7.3
I enabled
extension=pdo_mysql
in php.ini
in file=> config\packages\doctrine.yaml
dbal:
dbname: database
host: localhost
port: 1234
user: user
password: secret
driver: pdo_mysql
# if the url option is specified, it will override the above config
url: mysql://db_user:db_password#127.0.0.1:3306/db_name
# the DBAL driverClass option
driver_class: App\DBAL\MyDatabaseDriver
RiggsFolly brought the solution :
I have a second php.ini file in UwAmp. The extension pdo_mysql was disabled in bin/php/php-version/php.ini
I have just installed UwAmp 3.1.0 on a Windows 10 machine, and faced this same issue when trying to run the php artisan migrate command on a Laravel app. Here's how I solved it:
Go to bin/php/<php-version>
Make a copy of php_uwamp.ini named php-cli.ini
Open it on a text editor
Replace extension_dir = "{PHPEXTPATH}" by extension_dir = "ext"
Replace zend_extension = "{PHPZENDPATH}/php_xdebug.dll" by extension_dir = "../zend_ext/php_xdebug.dll"
Every time you change PHP Config on UwAmp interface, you must repeat these steps, since the changes are stored on php_uwamp.ini.
in the file php.ini there is the extension extension=pdo_mysql that is disabled. I enabled it and the connection worked.

Symfony with MSSQL (sqlsrv) results in timeout

I am trying to connect Symfony 2.8 to Microsoft SQL Server 2012 by using sqlsrv extension (no pdo sqlsrv, because for php7 - which I am using - there is currently only non pdo).
Trying to run cache clear or access the site in browser results in
"PDO::__construct(): MySQL server has gone away".
When I run the doctrine schema update command, everything works fine and the tables are being generated. Does anyone know how to fix the error? (I don't even know why it says Mysql server gone away when I try to connect on microsoft sql..)
Config:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: sqlsrv
host: testhost
dbname: testdb
user: testuser
password: testpw
mapping_types:
timestamp: string
I am looking for a solution since Friday but was not able to find one. I tried to select some entries from a seperate (non symfony) php skript, and there I got the results and no timeout. Why is Symfony / Doctrine giving me this strange error?
you can try to launch this command for doctrine :
php app/console doctrine:cache:clear-metadata
php app/console doctrine:cache:clear-query
php app/console doctrine:cache:clear-result
Also perhaps upgrade and downgrade your doctrine bundle for restore a correct and initial version of bundle for your project.

Categories