user7401552
user7401552

Reputation:

Mysql connection refuse in docker

So i am creating a new symfony 4 project but i cant get mysql to connect to my docker mysql. what part am i doing wrong?

Ive tried change mysql versions, composing it down and updating different mysql root and password and is not connecting.

here is my docker-compose.yml for mysql.

mysql:
        image: mysql:8
        container_name: sf4_mysql
        volumes:
            - .docker/data/db:/var/lib/mysql
        command:
            - "--default-authentication-plugin=mysql_native_password"
            - "--lower_case_table_names=1"        
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: sf4
            MYSQL_USER: sf4
            MYSQL_PASSWORD: sf4

Here is mysql Url in my .env DATABASE_URL=mysql://sf4:sf4@sf4_mysql:3306/sf4

every-time i do ./bin/console doctrine:database:create, connection is refused.

just trying to create a database.

controller

class HospitalAdminController extends AbstractController
{
    /**
     * @Route("/admin/hospital/new")
     */
    public function new(EntityManagerInterface $em)
    {
        $hospital = new Hospital();
        $hospital->setName('Example Hospital')
            ->setPhone(8175831483)
            ->setAddress('123 Avenue');

        $em->persist($hospital);
        $em->flush();


        return new Response(sprintf(
            'Hiya! New Hospital id: #%d phone:%s address%s',
            $hospital->getId(),
            $hospital->getPhone(),
            $hospital->getAddress()

        ));

}

Upvotes: 1

Views: 116

Answers (1)

Jimmix
Jimmix

Reputation: 6556

You need to expose mysql port. default is 3306. see port publishing

and more examples specific to mysql-8 on docker

For checking if mysql is running it is good to run inside terminal its client so you minimize potential error source to the database itself not including potential errors in php.

Upvotes: 1

Related Questions