Working with Doctrine DBAL in Symfony 4 with the following conofiguration (doctrine.yaml):
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)%'
driver_class: App\DBAL\Driver\PDOMySql\Driver
and .env:
DATABASE_URL=mysql://root:passwd#127.0.0.1:3306/dbname
we are unable to connect to the database via DBAL:
$conn = DriverManager::getConnection(Constants::connectionParams,new Configuration());
Getting An exception occurred in driver: could not find driver exception. I haven't found documentation about that error in this version in S4.
Looks like you are missing the PDO mysql extension.
Try to install it like below :
If you are running linux with apache2 do the following:
apt-get install php-mysql
After the above command has finished edit you php.ini file like so :
Search for pdo_mysql extension
It will be something like this
;extension=pdo_mysql.so
Change this line to this:
extension=pdo_mysql.so
And after changing php.ini file, restart apache service like this:
service apache2 restart
PS: you may need to use sudo
Related
I'm new to the Symfony and Doctrine world.
So i'm following the doc to install Symfony. All went well.
Next Step : create the DB. Again I'm following the documentation (Databases and the Doctrine ORM) and run the command php bin/console doctrine:database:create
This command give me no output and is running for more than 30 minuts which seems to be a lot just to create a DB.
I've got a local mysql server running on 127.0.0.1:3000
here is my .env file
###> 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&charset=utf8"
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:root#127.0.0.1:3000/my_project?serverVersion=8.0.19
###< doctrine/doctrine-bundle ###
Does anyone have a clue why this command does not give me any output ?
Problem solved even if I don't understand why.
I edit the doctrine.yaml that was like
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
to
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: my_project
user: root
password: root
host: localhost
driver: pdo_mysql
server_version: '8.0.19' # your database server version here
#url: '%env(resolve:DATABASE_URL)%'
Error:
my question may seems silly, but i don' t know to do that.
I created a symfony project and want to connect it to sqlite database.
Here is my doctrine config
I also set up the DATABASE_URL var.
However, when i want to generate migrations for example, i get the error could not find the driver.
I precise i installed sqlite by downloading a zip file.
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_sqlite'
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: true
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
IF anyone knows how to do it, i would be very thankful :)
Before using sqlite in wamp, you should enable exension php_sqlite3.
Enable extension in wamp context menu, php extension -> php_sqlite3 and restart wamp
Either your driver isn't installed properly, run php -m to see if it appears in the list or you need to restart your web server (even if it's the embedded php-symfony web server)
I get an error like database operations using Symfony2.
SQLSTATE[HY000] [2002] Connection refused
parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: symfony
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: tr
secret: ef9b4381fe75208f060b7c786951242bebcfb3c2
database_path: /private/var/mysql/mysql.sock
And console:
Kemal-Karakass-MacBook-Pro:~ kemalkarakas$ locate mysql.sock
/private/var/mysql/mysql.sock
How do I resolve the error?
i had the same issue and fixed it changing
database_host: 127.0.0.1
to
database_host: localhost
in parameters.yml
hopefully this helps
On Mac OSX using Mamp Pro, what solved the problem for me was going to the MySQL tab on the UI and checking the option Allow network access to MySQL
Don't forget to click Save to update the settings
There is a parameter unix_socket you can use within your config.yml.
See full configuration example:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
dbname: %database_name%
user: %database_user%
host: %database_host%
password: %database_password%
unix_socket: /tmp/mysql.sock
I had the same problem using MAMP because the default port is not 3306 but 8889.
You can find that information in the MAMP starting web page.
My parameters
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: symfony_blog
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 7f03537e81f981683bc773b9ec7113ab5861adec
database_path: null
This is a bit stupid but I arrived on this post googling the same error, so maybe this can help someone.
In case you have this Connection refused Error double check that your database is up and running, in my case I simply forgot to power on MAMP...
Had this issue "SQLSTATE[HY000] [2002] No such file or directory" on symfony 3.2.9 on deployment
I fixed it by changing the database_host value from "localhost" to my server IP on my parameters.yml file
but I had this another error message when trying to run commandline by SSH:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
I finaly fix it by adding these 2 lines on my config.yml file in Doctrine configuration section :
unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'
all my final doctrine configuration is like this:
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'
charset: UTF8
hopefully this helps
After seeing the answers and playing around with this myself I figured out the problem:
The default installation of LAMPP, MAMPP has the following in the my.cnf file:
skip-networking
This means that mysql will not listen on TCP/IP so you can tell doctrine to try any port in the 64K and it won't do any good until you comment out the directive above and get mysql to listen to TCP/IP ports.
Please ensure you restart LAMPP, MAMPP or reload mysql after commenting out the skip-networking directive.
If anyone is still stuck on this issue when using a virtual environment, I found if you are attempting to run database commands on the host machine and running into the "connection refused" error, try ssh-ing into your guest machine and running the commands directly so the path to mysql.sock is correct.
(I guess it makes sense that the relative path needs to be on the virtual guest machine, not the host machine).
Configuring symfony2/doctrine to connect to MySQL using host:port
comment out doctrine.dbal.path: %database_path% in app/config/config.yml
remove/comment out the parameter database_path from app/config/parameters.yml
check your configuration files for correct indentation
wrap either none of the values in app/config/parameters.yml single quotes or put all in double quotes.
Afterwards your configuration should look like this:
Parameters
# app/config/parameters.yml
parameters:
database_driver: "pdo_mysql"
database_host: "127.0.0.1"
database_port: "8889"
database_name: "symfony"
database_user: "root"
database_password: "root"
# comment out or remove
# database_path: ~
Configuration
# app/config/config.yml
# ...
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
# path is for pdo_sqlite: i.e. %kernel.root_dir%/data/data.db3
# comment it out !
# path: ~
Running on MAMP, this is what configuration of /app/config/parameters.yml that worked for me:
parameters:
database_host: '127.0.0.1'
database_port: '8889'
database_name: test
database_user: root
database_password: root
I lost quite some time with database_host: localhost, which didn't work.
Beginners Tips:
Don't forget to remove the semicolon ; to enable extension=php_pdo_mysql.dll in php.ini (in application/MAMP/conf/phpX.X.X - check your current version in MAMP settings)
and/or in php.ini and/or php.ini.default in Macintosh HD/private/etc (use spotlight search to find this hidden file).
Today on a fresh installation of Symfony3 I had the same error:
SQLSTATE[HY000] [2002] Permission denied
Installation specs:
CentOS Linux 7.2.1511
Symfony 3.1.2
MariaDB 5.5.47
PHP 7.0.8
An earlier answer by izus did resolve the problem on my server. He suggests to change the parameter database_host from 127.0.0.1 to localhost in the Symfony configuration file app/config/parameters.yml
The real origin of the problem was not inside Symfony configuration, but authorization inside the database server. I've created the database and authorization for this database by executing the following SQL queries:
CREATE DATABASE user CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON database.* TO user#localhost IDENTIFIED BY 'password';
This query implies only the host 'localhost' is authorized. This doesn't match '127.0.0.1' exactly. The database server rejects the incomming connection, and Symfony is throwing an exception and showing this error message.
Possible solutions to this specific situation:
Change the database_host parameter
Change the authorization inside the database server
In MAMP PRO I have activated "Allow Network Access to mysql" and my "php artisan migrate" is working now for that special app.
In a Symfony2 project, you can configure the databases connections at the app/config/parameters.ini file. Documentation states that you can use, among others, sqlite3 PDO driver.
But configuring sqlite doesn't works well:
[parameters]
database_driver = pdo_sqlite
database_host = localhost
database_port =
database_name = test_project.db
database_user = root
database_password =
Using app/console doctrine:database:create, successfully creates a test_project.db file at the project root directory.
But after creating some entities, then running app/console doctrine:schema:update --force should create the tables on the database file, but it doesn't, file appears empty, with O bytes size.
Note that using any other PDO driver works well, but not with SQLite...
I've also tried to use the full path for the db file in the database_name parameter, but to no avail, database still doesn't gets updated.
For reference, here's the doctrine dbal section of the config.yml file:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
Is there a way around this? configurations missing? something not stated on the official doc of symfony2 project?
According to Doctrine the elements used for sqlite DBAL configuration are:
user (string): Username to use when connecting to the database.
password (string): Password to use when connecting to the database.
path (string): The filesystem path to the database file. Mutually exclusive with memory. path takes precedence.
memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.
This is also listed in the full reference for Doctrine configuration in Symfony2, although not elaborated on.
So you need to switch up your config params to match whats appropriate for sqlite.
Here is what I needed to get SQLite to work, just after doing symfony new myapp :
in app/config.yml :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_sqlite
path: "%database_path%"
In app/config/parameters.yml:
parameters:
database_path: "%kernel.root_dir%/db/myapp_%kernel.environment%.db3"
...
Next I could do a composer install, create a new entity and it just worked.
I've found that if I add a path line pointing at the database_name to my config.yml, sqlite seems to pick that up, and MySQL doesn't seem to complain.
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
path: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
This means you can still keep all database information in the parameters file, you don't need separate configs depending on which database you are using.
Mainly the file path or the file path permisssion will have issue.
In config.yml, set path to full path like
/home/{name}/NB/PHP/Symfony/test/src/Database/data.db3
Dont give %database_path% or what ever. Try this it will work.
If it works you can give as
%kernel.root_dir%/../src/Database/%database_path%
Also check sqlite is ok by
phpinfo(INFO_MODULES);
In view/output you can see pdo_sqlite and its version.
In my case setting a username and password in config/packages/doctrine.yaml did not create a username/password protected sqlite database.
doctrine:
dbal:
charset: UTF8
url: '%DATABASE_URL%'
user: 'foo'
password: 'bar'
It seems like the parameters username and password are ignored?
To speed up the unit tests I want to use SQLite instead of MySQL, so I'm trying to get a SQLite-compatible dump of the databases from Propel's schema.xml. The application is built in Symfony2, and this is how the db configuration in config.yml looks like:
propel:
dbal:
driver: sqlite
dsn: sqlite:/tmp/test_db1.sq3
default_connection: db1
connections:
db1:
user: %database_user%
password: %database_password%
dsn: sqlite:/tmp/test_db1.sq3
db2:
user: %database_user%
password: %database_password%
dsn: sqlite:/tmp/test_db2.sq
However, the command app/console propel:build-sql is always generating MySQL-specific dumps that fail to load into SQLite.
I've also removed any references to MySQL from properties.ini and still don't get it right.
Any ideas?
Each connection name has to be related to a database name, so if you declare a connection c1, you need to have a database tag with the attribute name="c1" (in your schema.xml).
Assuming you have the following schema.xml:
<database name="default">
…
</database>
Write the following section in your config.yml:
propel:
dbal:
default_connection: default
connections:
default:
driver: mysql
username: root
dsn: mysql:host=localhost;dbname=my_db
And the following definition in config_test.yml:
propel:
dbal:
connections:
default:
driver: sqlite
dsn: sqlite:/tmp/test_db1.sq3
Then, if you want to generate SQL statements for your tests, just run:
php app/console --env=test propel:build-sql
To generate SQL statements for your dev env:
php app/console propel:build-sql
or
php app/console --env=dev propel:build-sql