Doctrine won't connect to MAMP mysql database - php

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.

Related

How to use mysql instead of mariadb in symfony 5

i think this might be a stupid question for some, but i swear that i can't find the answer by myself actually.
I'm still learning Symfony so please be nice.
Actually i have set up the access to my database in the .env file :
[...]
DATABASE_URL=mysql://root:db_password#127.0.0.1:3308/db_name
[...]
And then i have define the environnement variable in the config/doctrine.yaml :
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: Symfony
user: root
password: null
host: localhost
server_version: '8.0'
driver: pdo_mysql
charset: UTF8
So i run the following command :
> php bin/console doctrine:database:create
But the 'Symfony' database have been created under mariadb.
:( I don't understand why because i have define precisely that i use a 8.0 version and this version is not a mariadb version.
I've also try this :
# .symfony/services.yaml
mydatabase:
# supported versions: 5.7, 8.0
type: oracle-mysql:8.0
disk: 1024
As the symfony documentation saying to do, but it still not working at all.
I have got the following error message as mydatabase is not a symfony services :
C:\Users\vincp\Symfony\charming_tuto>php bin/console doctrine:database:create
In FileLoader.php line 173:
There is no extension able to load the configuration for "mydatabase" (in "C:\Users\vincp\Symfony\charming_tuto\con
fig/services.yaml"). Looked for namespace "mydatabase", found ""framework", "sensio_framework_extra", "twig", "twig
_extra", "web_profiler", "monolog", "debug", "webpack_encore", "doctrine", "doctrine_migrations", "maker"" in C:\Us
ers\vincp\Symfony\charming_tuto\config/services.yaml (which is being imported from "C:\Users\vincp\Symfony\charming
_tuto\src\Kernel.php").
I wanna use mysql because this is the DBMS that i use usually.
(If you think that i should use mariadb instead of mysql i'm open to that, but give me some arguments. I'd be really interseted in)
Maybe it is a server issue. So first, make sure that MySQL is running on your server besides MariaDB (how to do it depends on the type of server you are using).
If yes, it might be a port setting issue: I see that you set the database port to 3308. That is probably the port on which MariaDB is listenning. Try to determine if there is another db running on your server listening to another port (for example 3306 is very often the default MySQL port). To do so, on a Linux server you could try the following command :
netstat -tlnp | grep mysql
If you find only x x.x.x.x:3308 address or so, I bet that MySQL is not running on your server and you only run a MariaDB instance. But if you find addresses on other ports (for example x x.x.x.x:3306) chances are that a MySQL instance is running and listens that other port. If that's the case, you should consequently change the DATABASE_URL in your .env file.

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

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.

How to import existing database to homestead?

I'm learning to use Homestead 2.0. I have a existing MySQL database on my local machine. If I create a new laravel 5.0 project on homestead virtual machine, what should I do to connect to existing database?
If I need to migrate database on Homestead, how should it be done?
If I can connect to my database from Homestead, how shall I configure it on Homestead?
Thanks.
I had the same problem; this is how i resolve it
I was developing a laravel 4.2 project with WampServer as development environment, later I decided to change to laravel homestead as development environment
Steps:
Save your database as .sql file (my case: exported from phpMyAdmin and saved as db.sql)
Put the .sql file in the root of your project
Navigate to your homestead directory
C:\Users\chebaby\Homestead (master)
Get into the vagrant box
vagrant ssh
Navigate to your project root within the box (where you saved early the darabase .sql file in my case: db.sql)
vagrant#homestead:~$ cd Code/project-folder/
Login to mysql
vagrant#homestead:~/Code/project-folder$ mysql --user=homestead --password=secret
to check if your database already exist run
mysql> show databases;
Else create your database by running this command
mysql> create database yourdatabasename;
Now that you are sure your database is created, exit mysql back to the vagrant prompt
mysql> exit;
Imports the database file by running this
vagrant#homestead:~/Code/project-folder$ mysql --user=homestead --password=secret yourdatabasename < db.sql
All you have to do is to check if your existing database is succesfuly imported
Login to mysql
vagrant#homestead:~/Code/project-folder$ mysql --user=homestead --password=secret
then run
mysql> use yourdatabasename
To show the tables run
mysql> show tables;
hopefully this answers your question
Additional Resources
Laravel Homestead- MySQL default credentials and database
How to import an SQL file using the command line in MySQL?
Treat your Homestead Installation as if it's a remote host operating on a completely different machine. Homestead doesn't know anything about your localhost, and therefore cannot connect directly to your locally hosted MySQL databases.
Your Homestead installation operates on its own IP address. Whatever IP that is (likely 192.168.10.10), you can connect to it using MySQL Workbench or SequelPro using the following credentials:
Host: Your Homestead IP
Port: 3306
Username: homestead
Password: secret
You can find your IP by opening the following file:
~/.homestead/Homestead.yaml
If you're on Windows, then it would likely be:
C:\Users\your-username\.homestead\Homestead.yaml
In order to interact with your database, make sure Homestead is running first by running vagrant up in your Homestead directory, otherwise it will error out.
After that, you can export your local database and import it back into one of your Homestead databases using the credentials described above.
Note that Homestead is a separate environment and cannot access the DB on your host machine. Laravel Homestead comes with a mysql database installed. You should use this mysql for the code running in the Homestead. One easy way to solve your problem is to install phpMyAdmin on Homestead box and and then import the DB to mysql in Homestead.
Steps to install phpMyAdmin:
Got the below answer from here and added my notes
Download latest version of phpMyAdmin from https://www.phpmyadmin.net and extract it into a directory in your host machine.
Note: I extracted to /var/www/phpmyadmin/public. Here public directory is important otherwise you will receive No input file specified. error when you access phpmyadmin.
Configure homestead.yaml file to add a site where you can access phpmyadmin
folders:
- map: /var/www/phpmyadmin
to: /home/vagrant/code/phpmyadmin
sites:
- map: phpmyadmin.app
to: /home/vagrant/code/phpmyadmin/public
Add this line to your \etc\hosts file:
192.168.10.10 phpmyadmin.app
Note: 192.168.10.10 is the ip in my homestead.yaml configuration file.
Now start the vagrant by running vagrant up and you should be able to access the mysql of homestead by going to the url phpmyadmin.app. Now that phpmyadmin is installed you can import your DB easily.

Symfony2 database configuration during deployment

hi i'm new to symfony2 web development. I have develop a sample symfony2 application with a database on my localhost. It is working fine on my localhost.
I have uploaded my project to web server, All the files are working on the web server other than the files connecting with databases. When I requested for the files that are connected with database I got these errors.
Warning: PDO::__construct(): MySQL server has gone away in
/home/pipeline/public_html/BusinessPipeline/BusinessPipeline/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
on line 40
Warning: PDO::__construct(): Error while reading greeting packet.
PID=18547 in
/home/pipeline/public_html/BusinessPipeline/BusinessPipeline/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
on line 40
I have setup the parameters.yml file as follows.
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_name: test_project
database_user: my_username
database_password: my_password
I didn't change anything rather than change the parameters.yml.
what can I do for this issue, do I need to do any configuration prior to deployment?
Thanks in advance
Check MySQL Server doc for specific problem about your error message:
B.5.2.9 MySQL server has gone away
Check the wait_timeout problem

Symfony2 Database Config

I've just inherited a Symfony2 project from a former co-worker. I'm trying to get it running locally, but am getting a SQLSTATE[HY000] [2002] Connection refused error. I guess I'm missing the database connection that I've read should be in the app/config/parameters.yml file.
Is there anyway of finding this information if all I have is the project and the live site?
If it's standard Symfony2 project you have to configure your database connection information in app/config/parameters.yml, like:
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_name: test_project
database_user: root
database_password: password
# ...
More in doc.
After that you have to create database:
$ php app/console doctrine:database:create
Next, create table schema:
$ php app/console doctrine:schema:update --force
Also, you could load (if your co-worker provided it ; ) some data fixtures, so try:
$ php app/console doctrine:fixtures:load
If your coworker doesn't use doctrine fixtures you should consider dumping data from your prod database.

Categories