All I want to do is create a new database off of a dump created by phpmyadmin.
This dump is located in : /var/www/iadmin/wikifresh/db/template.sql and won't ever change.
I ran this command once, and the database was created fine
mysql test < /var/www/iadmin/wikifresh/db/template.sql
and it created the database. So, I put that script inside of a php exec command:
(note: $wikiname is the name of the new wiki being created)
$dbwikiname = escapeshellarg($wikiname);
exec("mysql $dbwikiname < /var/www/iadmin/wikifresh/db/template.sql");
now, when this script runs, I get:
ERROR 1049 (42000): Unknown database 'test'
even if I try to run it from my command line I have this issue.
what am I doing wrong?
For mysql to be invoked with a default database name, the database must exist.
Some distributions of MySQL happen to ship with a database called test, which explains why your command succeeded at first. One presumes you have since dropped that database? You will need to recreate it before attempting to select it:
CREATE DATABASE IF NOT EXISTS test;
Of course, you could instead place the above command, followed by USE test; atop your template.sql file and then invoke mysql without specifying a default database.
it seems you don't have the database 'test' created in the MySQL server of that machine, and the template.sql doesn't have the 'CREATE DATABASE' command. So you have to either create it first, or add the 'CREATE DATABASE' to the template.sql
Related
I'm running the yii cache/flush-schema command on the root of yii-advanced project but it's working on only one and default db connection. Whereas, the application is having multiple database connections and I need to run the command on specific database.
Running the command with specifying the database connection name in it, would do the job, as:
yii cache/flush-schema db_connection_name
This does not work:
bin/console doctrine:query:sql "SET FOREIGN_KEY_CHECKS=0" --env=test
I can execute the SQL part of the command via MySQL Workbench. On the console, it will just give me:
/var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php:71:int 0
Which could possibly be the correct return value as no columns are affected.
But when I do SELECT ##FOREIGN_KEY_CHECKS it is still set to 1.
To clarify: I use the same user to perform these operations via console as via MySQL client.
It seems that changing this setting does only apply to the current session, which will be closed directly after the command is executed.
I've one 'xyz.sql' dump file uploaded in 'test' folder on the server.
Created a blank database called 'dummy' in MySQL database using PHPMyAdmin.
Then I logged into the server using ssh and run the following command, it gave me no error but the database is not getting imported.
mysqldump -uroot -pxyz123 new_db > xyz.sql
The username I use to login to the PHPMyAdmin is root and password is xyz123. The blank database I created is 'new_db', the file which I want to import is xyz.sql.
You can see in above command I've used these details.
Try using < instead of > when you import the dump. First check if xyz.sql still contains your dump!
Scenario:
I have built a PHP framework that uses a postgresql database. The framework comes shipped with a .sql file which is a dump of the default tables and data that the framework requires.
I want to be able to run the sql file from the client (PHP), rather than the command line, in order to import the data. This is because I have come across some server setups where accessing the command line is not always a possibility, and/or running certain commands isn't possible (pg_restore may not be accessible to the PHP user for example).
I have tried simply splitting up the .sql file and running it as a query using the pg_sql PHP extension, however because the dump file uses COPY commands to create the data, this doesn't seem to work. It seems to be that because COPY is used, the .sql file expects to be imported using the pg_restore command (unless I am missing something?).
Question:
So the question is, how can I restore the .sql dump, or create the .sql dump in a way that it can be restored via the client (PHP) rather than the command line?
For example:
<?php pg_query(file_get_contents($sqlFile)); ?>
Rather than:
$ pg_restore -d dbname filename
Example of the error:
I am using pgAdmin III to generate the .sql dump, using the "plain" setting. In the .sql file, the data that will be inserted into a table looks like this:
COPY core_classes_models_api (id, label, class, namespace, description, "extensionName", "readAccess") FROM stdin;
1 data Data \\Core\\Components\\Apis\\Data The data api Core 310
\.
If I then run the above sql within a pgAdmin III query window, I get the following error:
ERROR: syntax error at or near "1"
LINE 708: 1 data Data \\Core\\Components\\Apis\\Data The data api Core...
This was a bit tricky to find, but after some investigation, it appears that pg_dump's "plain" format (which generates a plain-text SQL file) generates COPY commands rather than INSERT commands by default.
Looking at the specification for the pg_dump here, I found the option for --inserts. Configuring this option will allow the dump to create INSERT commands where it would normally create COPY commands.
The specification does state:
This will make restoration very slow; it is mainly useful for making dumps that can be loaded into non-PostgreSQL databases. However, since this option generates a separate command for each row, an error in reloading a row causes only that row to be lost rather than the entire table contents.
This works for my purposes however, and hopefully will help others with the same problem!
I have a PHP script that does the following in the order presented:
Connect to a MySQL database
Retrieve a single row of data
Close the MySQL database connection
Connect to a SQLite database on the local file system
Insert the row of data into the SQLite database
Close the SQLite database connection
I'm using PDO as the vehicle for both MySQL and SQLite. Here is the code for the insert:
$sqlite = new PDO('sqlite:activity.sqlite');
$sqlite->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$lite = <<<LITE
INSERT INTO Transactions (Date,Transactions,ActiveMembers,Amount)
VALUES
('$day',
'$trans',
'$active',
'$amount');
LITE;
try
{
$sqlite->exec($lite);
}
catch (PDOException $e)
{
die($e->getMessage());
}
(please forgive the usage of variables in the query, rather than a proper prepared statement; I removed the bound parameters to see if that was the problem)
Using other tools (SQLite Manager for Firefox, SQLite Database Browser for Windows), I am able to access the database and write to it.
This script is the only thing that touches this particular database. The permissions on the database file are 777. Nothing else has it, or its containing directory, open when the script runs.
When running the script (using php -f), the portion that tries to insert into the SQLite database creates the journal file, takes about 10-15 seconds, and then returns the error SQLSTATE[HY000]: General error: 5 database is locked.
So my question, then, is this:
Is there something about running a PHP script from command line that prevents interaction with a SQLite database? If so, what? If not, what could be the issue here?
I have come across the same problem. After using the fuser command and finding which process had created a sticky lock I knew the rogue:
[root]# fuser cms.db
cms.db: 4511
[root]# ps aux | grep 4511
nobody 4511 0.0 3.2 74560 25160 ? S Oct13 2:00 php-fpm
[root]# /etc/init.d/php-fpm restart
PHP-fpm was the guilty one so restarting the service did the trick. That must have been a bug in the PHP version I was running (5.4) and may still be.
Be sure to clear your connection to SQLite, i.e. after your code, set
$sqlite = null;
See http://php.net/manual/en/pdo.connections.php