I'm doing some mysql server management with a script that flushes the MySQL users privileges when new privileges are added to a MySQL user.
I'm using the PDO class to do my queries, but when I do a simple
FLUSH PRIVILEGES;
I get, for
$connection->exec('FLUSH PRIVILEGES;');
and
$connection->query('FLUSH PRIVILEGES;');
SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'mysql.servers'
doesn't exist
Is it possible to do such query with the PDO class or do I have to resort to using mysql(i)?
I've just tried the following portion of code :
$dsn = 'mysql:dbname=mysql;host=127.0.0.1';
$user = 'root';
$password = '********';
try {
$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->query('flush privileges;');
} catch (PDOException $e) {
var_dump($e);
}
And I don't get any kind of error like the one you are describing.
Are you sure you don't have some problem with you MySQL server ?
Your error message says that table "mysql.servers" doesn't exists... But when I look at my local MySQL server, there is such a table -- are you sure your installation/configuration is not "broken" and you didn't delete that table or anything like that ?
BTW, it doesn't seem to be some kind of privilege you're not having : if you try to flush privileges without having the required privilege, you get the following error : "SQLSTATE[42000]: Syntax error or access violation: 1227 Access denied; you need the RELOAD privilege for this operation"
Related
when I type php artisan migrate in terminal
I get this error number [2054] and sometimes [1054] !
I tried to change everything like host, username and password in .env file
database.php and config.php file, but it didn't work well
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select * from information_schema.tables where table_schema = lsapp and table_name = migrations)
at /Users/Ali/education/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
The error says you do not have permission to do queries. You need to access with the password and/or the database must have a password.
Verify the access codes (user, password) or try to access by a web manager, like phpmyadmin, pgadmin, etc ...
Finally, I got the solution by downloading MySQL Workbench then create a new database in this app , and it's working fine
First of all I don't have any database and I write code in migrations to create database, table and data, I want to check if database not exist then auto create new database have name in db connect and try catch this connect if error.
public function up()
{
try {
\DBUtil::create_database('database_name', 'utf8_general_ci', true);
// the true flag adds a IF NOT EXISTS
} catch(\Database_Exception $e) {
echo $e->getMessage();
}
}
try code but give error "Unknown database database_name" when I run "php oil refine migrate" in commandline
Can I try catch custom exception this error?
1049!
Fuel\Core\Database_Exception [ 1049 ]:
SQLSTATE[HY000] [1049] Unknown database 'database_name'
Creating a database is not something that is usually done by migrations as it would tie you in to using the same DB name all the time, if you are deploying multiple sites on the same server or have a similar scenario this becomes a big problem.
It's generally assumed that the database itself has been created as part of the set-up, either manual or automatic, so is available for PHP to connect to.
I'm trying to create a database using the following code:
// Set up DB connection and creates the DB
try {
$connection = new \PDO(
'mysql:host='.Settings\Database::$host.';',
Settings\Database::$username,
Settings\Database::$password);
$connection->exec("CREATE DATABASE IF NOT EXISTS ".Settings\Database::$databaseName." CHARACTER SET utf8 COLLATION utf8_unicode_ci;");
} catch (\PDOException $exception) {
die("Could not connect to database: ".$exception->getMessage());
}
The problem is that no database is being created and I receive no error except for the fact that when I try create a table with PDO i receive this error:
READ EDIT 2
Could not connect to database: SQLSTATE[HY000] [1049] Unknown database 'dbname'
Edit:
I have no problem manually creating the DB with phpMyAdmin and similars.
Edit 2:
Mistakenly I thought that the error was given by the CREATE TABLE... statement. Instead the error is returned by the die() function in the exception handling.
Your call to exec() isn't throwing exceptions. You have to enable that for each PDO connection with an attribute like this:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
If you were getting the error message from exec(), you would have seen this:
Could not connect to database: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLLATION utf8_unicode_ci' at line 1
The syntax for CREATE DATABASE uses the keyword COLLATE, not COLLATION.
See http://dev.mysql.com/doc/refman/5.6/en/create-database.html
I have been researching this for the past hour, and yet to come up with a simple solution that doesnt involve some weird exports/imports.
All I am trying to do is open up a PDO connection with two databases so that I can use them both in queries.
It seems that there is disagreements in Stack Overflow about this.
One answer:
...you will need to create two PDO objects for the seperate
connections if you would like to use both at runtime.
But others seem to suggest you can just "use" two databases in your query:
$sql = "SELECT * FROM dbname.tablename";
$sql = "SELECT * FROM anotherdbname.anothertablename"
I tried preforming a SELECT command on another database besides the one explicitly defined in my PDO connection function. I got this:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT
command denied to user 'dbusername'#'localhost' for table
'table_name'
I made sure to add the user to both databases and grant full privileges.
Is a query that uses two databases in the same connection possible? Or do you have to setup two different objects?
"a PDO connection with two databases" (singular form) is a misnomer, because by definition a PDO connection is a single connection to a single data store. If you want two connections, you will need to instantiate two instances.
Turns out preforming a query across two databases is possible with a single PDO connection. Even though one database is defined in my PDO initiation, I still have access to another database.
The solution is to make both databases have the same credentials.
function db_connect(){
$host = 'localhost';
$port = 3306; // This is the default port for MySQL
$database = 'db1';
$username = 'user';
$password = 'pass';
$dsn = "mysql:host=$host;port=$port;dbname=$database";
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
$db=db_connect();
$statement = $db->prepare("SELECT * FROM db2.table LIMIT 1");
$statement->execute();
$x=$statement->fetchObject();
var_dump($x); //A full row from the table was the output.
I made sure to add the user to both databases and grant full privileges.
The error message is quite unambiguous. Reading this, I wouldn't be so sure.
Anyway, to answer the title question:
Why not to just run this simple query from the console?
INSERT INTO db2.table SELECT * FROM db1.table;
It will not only transfer your data but also will prove if your users have sufficient rights or not.
If not - you have to really make sure that.
I'm migrating my local files online and having PHP issues with my db script. Works fine locally (running PHP 5.3.8) but gives me the following errors on my server (PHP 5.3.10)
Without $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
Fatal error: Call to a member function setFetchMode() on a non-object in /.../...php on line 108
With $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user '...'#'205.186.180.26' for table 'invited'
Here is my code:
class guest {
public $id;
public $guest_name;
public $url_phrase;
}
$guest = new guest;
if (isset($_GET['p'])) {
$url_phrase = urldecode($_GET['p']);
} else if (isset($_POST['p'])) {
$url_phrase = urldecode($_POST['p']);
}
if (isset($url_phrase)) {
try {
$DBH = new PDO("mysql:host=myhost.com;dbname=mydbname", "myusername", "mypassword");
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$url_phrase = $DBH->quote($url_phrase);
$sql = "SELECT id, guest_name, url_phrase FROM mydbname.invited WHERE url_phrase = $url_phrase";
$stmt = $DBH->query($sql);
$stmt->setFetchMode(PDO::FETCH_INTO, new guest);
$guestNumber = $stmt->rowCount();
if($guestNumber > 0) {
etc...
}
if($guestType == "solo") {
foreach($stmt as $guest) {
$name = $guest->guest_name;
etc...
}
}
} else {
other stuff.. etc..
$DBH = null;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
I've gotten simple select statements to work fine as this user (I don't think its permission-related), my problem seems to be with my implementation of PDO. How can I get rid of this error? Am I preparing/executing the statement in a weird way that's messing this up? Thanks for any help
I had the same problem. After I uploaded my code from local to production server, and the user had all privileges. The problem was the database name -- in my local environment it was something like dattabase1 and in production the name was different...prefix_ddatabas1. I changed the name of the database to the correct one and everything was ok.
Sounds like an access issue on the server:
SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command
denied to user '...'#'205.186.180.26' for table 'invited'
Have you tested from the CLI (or some other sql client outside of PHP) to ensure your script will have access?
You will likely need to log into the db to give access to a user your script is running as or check the credentials you're using in the script are accurate for the db in the server environment.
I had same issue, code working at dev server but not at prod server, and other scripts working with same table were working. I had missed to correct database table prefixes between dev and prod environment. (the database's names were not the same) ^^
I received the same error. The dev DB name was different than the production DB name thus causing this error. I changed the name of the db in my code to the production DB name and it worked.
Thanks
You have an access violation:
Syntax error or access violation
The user/host you're using:
1142 SELECT command denied to user '...'#'205.186.180.26'
can not select from the table 'invited'