I am trying to get my Symfony 3 app running in production mode on an Amazon EC2 instance.
I am have the code in the correct place, and then I try to setup any dependencies with composer by running
export SYMFONY_ENV=prod
composer install --no-dev --optimize-autoloader
but get the error
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driverL SQLSTATE[HY000] [1044] Access denied for user 'my-user'@'127.0.0.1' to database 'my_database'
This error is then passed down to [Doctrine\DBAL\Driver\PDOException] and [PDOException] before the script terminates.
This is then followed by the message
[RuntimeException]
An error occured when executing the "'assets:install --symlink --rela'\''web'\'''" command:
which is then followed by the same errors as at the beginning.
My parameters.yml file is
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: my_database
database_user: my-user
database_password: mypassword
server_version: 5.6
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: Mysecret
registration_permitted: true
registration_limit: 3
and the user my-user has all permissions on the database my_database except GRANT.
I have been trying to solve this for a few hours to no avail, other solutions I came across involved misspelt usernames/passwords/databases (triple-checked) or needed the server version adding (which hasn't made a difference).
Additional Info
Running mysql -u my-user -p prompts for the password and then allows me to log in.
Running mysql -u my-user -p -D my_database prompts for the password and then returns ERROR 1044 (42000): Access denied for user 'my-user'@'localhost' to database 'my_database'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'; returns ERROR 1141 (42000): There is no such grant defined for user 'my-user' on host '%'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'@'localhost'; returns GRANT USAGE ON *.* TO 'my-user'@'localhost' IDENTIFIED BY PASSWORD hashedPassword.
Additional Info 2
After deleting my-user ('my-user'@'localhost' and 'my-user'@'127.0.0.1') and then recreating them using GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'@'localhost' IDENTIFIED BY 'password'; and also GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'@'127.0.0.1' IDENTIFIED BY 'password'; and then FLUSH PRIVILEGES; The output of SHOW GRANTS FOR 'my-user'@'localhost' is then
GRANT USAGE ON *.* TO 'my-user'@'localhost' IDENTIFIED BY PASSWORD 'passwordHash'
GRANT ALL PRIVILEGES ON 'my_database' TO 'my-user'@'localhost'
and similar output for 'my-user'@'127.0.0.1'.
But this is still giving me the same errors as before, any more ideas?
解决方案
As discussed in comments you did not have any privilege on the database.
So running this fixes your problem:
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'@'localhost'
About your doubt why you had to change it to localhost in your params because that's what you're telling mysql
Imagine you want to restrict database access to only a specific ip in your network you could run
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'@'192.168.1.10'
So replace 192.168.1.10 with 127.0.0.1 and you can use 127.0.0.1 in your parameters