"Syntax error or access violation: 1142" in Symfony and MySQL - php

I am creating an API REST aplication with Symfony3, and I have this problem. When I execute the methods in localhost it works fine, but I have uploaded the project to a server administered with Plesk, and when I try to execute the methods with postman, it shows this:
An exception occurred while executing 'SELECT t0.random_id AS ...'
with params [\"a\", \"a\"]:\n\nSQLSTATE[42000]: Syntax error or access
violation: 1142 SELECT command denied to user 'app'#'localhost' for
table 'table_name'
It seems to be a permissions problem, but if I try to access to the database with MysQL Workbech, with the same credentials, I can do it and execute querys without problems. And in the Plesk panel, the user has all the permissions and privileges.
I have also tried changing "localhost" for the host address in the parameters file of Symfony.

Looks like app user doesn't have permissions to execute SELECT query.
Run following MySQL query to fix it:
GRANT SELECT on database_name.* to 'app'#'localhost' identified by 'your-pass';
FLUSH PRIVILEGES;

Related

Laravel migrate command is not working for remote database

I am working on a project using the Laravel 4.2 framework. I want to execute the command php artisan migrate but when I run this command it shows an error:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'abc'#'10.1.1.27' for table 'migrations'
I think I have placed the project files and the database on different servers, so that's why am I getting this error.
Database server: 10.1.1.56
Project server: 10.1.1.27 (localhost)
This error indicates that the remote MySQL database isn't configured to allow certain operations executed by the user abc from the IP address 10.1.1.27. In many cases, MySQL users are set up to allow access from the the same host as the database server, but we need to explicitly GRANT access for clients connecting to the database from a remote host.
We can use the following commands to grant the user abc access from remote hosts. We must run these commands as a user that has the ability to grant privileges to other users (such as the database's root user):
GRANT ALL PRIVILEGES ON database.* TO 'abc'#'%';
FLUSH PRIVILEGES;
Replace database with the name of the application's database configured in .env or config/database.php. The wildcard '%' in 'username'#'%' indicates that we want to give permissions to that user from any IP address. If we don't want to allow the user access from any IP address, we can limit access to specific IP addresses by replacing the wildcard with the address to allow (10.1.1.27 in this case).
Depending on the needs of the project, we may not need to give a user all privileges to a database. See the documentation for a list of available privileges that we can assign.
1142 CREATE command denied to user 'abc'#'10.1.1.27' for table 'migrations'
The above command simply means that the user don't have CREATE permission on the connected database. So first of all you have to grant the privileges to that user on the database and after that run the migration.
Explanation: When you run migrate, a table is created on with the name migration in the database that maintains the status of migration ion it, and you don'r have the CREATE permission that's why it is showing error.

Can't connect to MySQL with a new MySQL user

I work on a personal website. I just put it on the production server. When I am in the development environment I connect to MySQL with root but now I just created a user specific for this website. So I wrote this command line in MySQL:
GRANT USAGE ON portefolio.* TO 'userPortefolio'#'localhost' IDENTIFIED BY PASSWORD 'myPasswordCrypted'
The query is ok, but on my website, when I try to access a page with a query, Symfony gives me that error:
SQLSTATE[42000] [1044] Access denied for user 'userPortefolio'#'localhost' to database 'portefolio'
I tried to restart the server to be sure everything is ok but nothing.
I encrypted the password with the MySQL function
password('my password not encrypt')
and in Symfony I wrote the password in non-encrypted format. Is that ok?
GRANT USAGE ON portefolio.* TO 'userPortefolio'#'localhost' IDENTIFIED BY PASSWORD 'myPasswordCrypted'
Your password should not be encrypted in this statement. MySQL crypts it automatically.
Assuming no quirks in connecting etc were made you should do a
flush privileges;
after running the grant query.
quoting from the manual: The USAGE privilege specifier stands for “no privileges.”
so everything seems to be working as designed.

xampp mysql: select command denied

I am using xampp om windows 7. I logged in as the root user with the password i have set up. I created a new database(I am using xampp for the first time). and I am not getting any option for creating any tables in it.
It shows the following error:
Error
SQL query: DocumentationEdit
SELECT `tables`
FROM `phpmyadmin`.`pma_recent`
WHERE `username` = 'root'
MySQL said: Documentation
#1142 - SELECT command denied to user ''#'localhost' for table 'pma_recent'
How do I get rid of this?
I get this line also, somewhere at the bottom of the page along with the error message:
The phpMyAdmin configuration storage is not completely configured, some extended features have been deactivated.
http://dev.mysql.com/doc/refman/5.1/en/grant.html
GRANT SELECT ON db.table TO 'user'#'localhost';
GRANT ALL ON db1.* TO 'jeffrey'#'localhost'; ------------ i think this is much better.

Why PHP MySql connection do not work?

I'm using MySQL given from A2Hosting. There I can create users and add them to databases. And i hosted my php codes inside web folder in symfony php structure. I dont want to use any symfony commands or functions. I have my own php calls to DB. But my Php codes gives the following error. But the user names and passwords are correct.
Query failed : Access denied for user 'games_user'#'localhost' to database 'games'
Granting privileges to user 'games_user' could not be enough. You must grant privileges to 'games_user'#'localhost', specifiying the host, even for localhost.
Granting all privileges for instance should look like this:
GRANT ALL PRIVILEGES ON database.* TO 'games_user'#'localhost' IDENTIFIED BY 'password';
Check permissions for user "games_user" in your "games" database. Access is denied to database "games"

MySQL Permissions for CRON job

I have written a PHP script to be executed by CRON on a daily basis to clean files and remove old database entries..
The script has no problems being run, so I know its not a CRON issue.
The problem is that I get the following error:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user 'username'#'localhost' for table 'users' in /cron_removeOld.php on line 35
I am using the same DB credentials for the backend admin users, so I know they have SELECT privileges. I'm wondering if it has something to do with the script being run outside of the webserver?
Any help would be greatly appreciated!
Cron:
20 0 * * * php -q /home/user/public_html/tssol/contract/admin/cron_removeOld.php
PHP (Line 35):
$stmt = $dbh->prepare("SELECT idusers, contract FROM users WHERE contractExpireDate <= NOW()");
The db creds are in an include file that is shared with other pages so I know thats not an issue..will post if you think it will help
MySQL permission has nothing to do with PHP executed by CLI or whatever.
Check if your mysql user has the SELECT provileges for users table

Categories