How can I connect via Symfony 4 to mySQL database created with MAMP?

symfony 4 connection refused
how to connect database in symfony 4
symfony database config
symfony doctrine
symfony get data from database
symfony doctrine tutorial
symfony 4 repository
symfony doctrine annotations

I created a database in MAMP called "project".

In my .env file I added this line:

DATABASE_URL=mysql://root:root@localhost:3306/project

Now I want to run

php bin/console doctrine:database:create

But I get an error message:

SQLSTATE[HY000] [2002] No such file or directory

my doctrine configurations:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

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

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

Solved it!

in the file "config/packages/doctrine.yaml" I had to add this line

unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock

This means change this:

 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

        url: '%env(resolve:DATABASE_URL)%'

into

 dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'

Installing Doctrine > Doctrine & the Database, Welcome back to the *third* episode in our starting in Symfony 4 series! We've done Open the README.md file for details on how to get the project setup and of course, a space poem. The last I got this error after php bin/console doctrine:database:create And MAMP should come with the PDO/MYSQL extensions. 1. There is a single MySQL user, so you can not use "DEFINER" Access Control mechanism for Stored Programs and Views. 2. MySQL Errors such as "PDO Exception 'MySQL server has gone away'" are usually simply the result of exhausting your existing diskspace. Be sure you have sufficient space allocated to the service in .symfony/services.yaml.


Got the same problem. Added the unix_socket and changed 127.0.0.1 to localhost works on MAMP 5.5.

doctrine.yaml

doctrine:
dbal:
    # configure these for your database server
    driver: 'pdo_mysql'
    server_version: '5.7'
    charset: utf8mb4
    unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
    default_table_options:
        charset: utf8mb4
        collate: utf8mb4_unicode_ci

    url: '%env(resolve:DATABASE_URL)%'

.env

DATABASE_URL=mysql://root:root@localhost:3306/mydb

Up & Running With Symfony 4, The Symfony server is part of the symfony binary created when you install Symfony the most appropriate PHP version according to the project which is running the commands. 1 2 3 4. # docker-compose.yaml services: database: ports: [3306] For instance, running symfony run psql will connect you automatically to the  i am using mysql v.5.7 with dbal v.2.5 even if the is server_version: '5.7' but i am still unable to create the database with the command [Doctrine\DBAL\Exception\ConnectionException] An exception occurred in driver: SQLSTATE[HY000] [1049] Unknown database 'symfony'


Try to change this:

url: '%env(resolve:DATABASE_URL)%'

to this:

url: '%env(DATABASE_URL)%'

Symfony Local Web Server (Symfony Docs), When using Apache, you can configure PHP as an Apache module or with 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/​db_name" a pool called www [www] user = www-data group = www-data ; use a unix  I created a database in MAMP called "project". How can I connect via Symfony 4 to mySQL database created with MAMP? meaning and mysql-client lib tries to


What you can also try is setting the mysql server expliclitly

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                dbname:         local_api
                user:           root
                password:       null
                host:           localhost
                driver:         pdo_mysql
                server_version: '5.5' # in case you are using mysql 5.5

Configuring a Web Server (Symfony Docs), For instructions on how to create a MySQL database using cPanel, see this  Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB. Databases are a broad topic, so the documentation is divided in three articles:


How to Connect PHP to MySQL Database with PDO and MySQLi, The following example shows how to connect to a MySQL database using the mysqli() library: <?php DEFINE('DB_USERNAME', 'root');  Fill in the installation fields using the information provided on the MAMP start page and the database you created in step 4 as well as the username you specified in step 10. Continue filling out the remaining fields during the installation. Refer to the INSTALL.txt, in the Drupal Root directory, for additional help. NOTE!!


MAMP (Mac) Documentation > How Tos > Connect to MySQL from , MAMP creates a local server environment on Mac OS X by installing PHP, MySQL, and Apache right out of the box. Leaving it as "Any host" will not allow Drupal to connect to your local database and you'll get an error. provided on the MAMP start page and the database you created in step 4 as well as  The databases.yml configuration allows for the configuration of the database connection. It is used by both ORMs bundled with symfony: Propel and Doctrine. The main databases.yml configuration file for a project can be found in the config/ directory.


HowTo: Create a local environment using MAMP, On Unix, MySQL programs treat the host name localhost specially, in a way that is likely To ensure that the client makes a TCP/IP connection to the local server, use Import database from dump with Doctrine and Symfony. How to Generate Entities from an Existing Database¶ When starting work on a brand new project that uses a database, two different situations comes naturally. In most cases, the database model is designed and built from scratch. Sometimes, however, you'll start with an existing and probably unchangeable database model.