I am attempting to use the sqlite backup command through php.
This requires 3 statements
Opening the db sqlite3 testing.sqlite
Backup the DB .backup testing_backup.sqlite
Close sqlite .exit
The exec command doesn't like this as it hangs when the process stays open, I have tried running all three commands together using && to join them but this doesn't work either.
Can anyone help me run the 3 commands through php?
This is an attempt to create a backup file to solve a database locking issue.
The sqlite3 command-line shall can also receive command(s) as parameters.
Just execute the following command:
sqlite3 testing.sqlite ".backup testing_backup.sqlite"
Related
I want to be able to dump a whole sqlite database to a file (database.sql) so as to use it as a backup. The problem is that exec() in PHP doesn't seem to run correctly the ".dump" command as it gives only "COMMIT;" as a result and not the whole text:
$db = new SQLite3('checks_db.db');
$results=$db->exec('.dump');
$content=$results->fetchArray();
If I run it from sqlite3 it gives both the schema and the insert insrtuctions with the data.
Perhaps I should also mention that the database has 777 permissions.
Can anyone write an example of the syntax of the '.dump' command in php-sqlite3?
The SQLite database does not have a .dump command.
The sqlite3 command-line shell has a .dump command.
To be able to use it, you would have to execute that tool; something like this:
exec('sqlite3 /some/where/checks_db.db .dump', $output);
The easiest way to make a backup would be to copy the database file itself, but using the shell's .backup command would be safer.
I have a 28 MB sql file need to import to mysql.
Firstly, i'm using xampp to import, and it fails, and so i change my max_file_uploads, post_size(something like that)in php.ini-development and php.ini-product to 40 MB, but it still show "max:2048kb" and import fail again.
From research, i've learned to import by using mysql.exe, so i open mysql.exe and type the command line(msdos) below:
-u root -p dbname < C:\xxx\xxx\sqlfile.sql
but still failed again and again.....
what the problem is? xampp? or my sql setting?
Try this:
mysql -uroot -p --max_allowed_packet=24M dbname
Once you log into the database:
source C:\xxx\xxx\sqlfile.sql
I think that you should be able to load your file
How large is your file?. You might as well do it from a console:
mysql -u##USER## -p ##YOUR_DATABASE## < ##PATH_TO_YOUR_FILE##
Do that without executing your mysql.ext file: just "cd" right into the directory and try the command.
It will ask for your password and start importing right away. Don't forget to create the database or delete all tables if it's already there.
I always found this approach quick, painless and easier that rolling around with php directives, phpmyadmin configuration or external applications. It's right there, built into the mysql core.
You should increase max_allowed_packet in MySQL.
Just execute this command before importing your file:
set global max_allowed_packet=1000000000;
I also fetched the similar problem. So after that I also conclude , large sql file will never be imported to mysql. It will always give timeout error.
Then I found a solution.
There is an software Heidisql.
follow below steps:-
1) download the software.
2) then install the software
3) create new session in Heidisql and open the session
4) then go to Tools -> Load SQL File -> Browse.
That's it. This solution works best for me.
check the link here
I found the only solution was to log in to MySQL from the command line and use the 'source' command:-
1) cd to the directory containing your SQL file for import, then log into MySQL:
#> mysql -u YOURUSERNAME -p -h localhost
2) use MySQL commands to import the data:
#> use NAMEOFYOURDB;
#> source NAMEOFFILETOIMPORT.sql
This also feeds back info about progress to your terminal, which is reassuring.
I'm using a php script to backup my sql databases remotely that utilizes mysqldump. http://www.dagondesign.com/files/backup_dbs.txt
and I tried to add the the --lock-tables=false since I'm using MyISAM tables but still got an error.
exec( "$MYSQL_PATH/mysqldump --lock-tables=false $db_auth --opt $db 2>&1 >$BACKUP_TEMP/$db.sql", $output, $res);
error:
mysqldump: Couldn't execute 'show fields from `advisory_info`': Can't create/write to file 'E:\tmp\#sql_59c_0.MYD' (Errcode: 17) (1)
Someone told me this file was the lock file it self and I was able to find it in my Server that I wanted to backup.
So is this the lock file? And does it lock the database if you do remotely no matter if I put the variable --lock-tables=false? Or should it not be there since there are a lot of people working on the server and someone might have created it?
It's likely --lock-tables=false isn't doing what you think it's doing. Since you're passing --lock-tables, it's probably assuming you do want to lock the tables (even though this is the default), so it's locking them. In Linux, we don't prevent flags but appending something like =false or =0, but normally by having a --skip-X or --no-X.
You might want to try --skip-opt:
--skip-opt Disable --opt. Disables --add-drop-table, --add-locks,
--lock-tables, --set-charset, and --disable-keys.
Because --opt is enabled by default, you can --skip-opt then add back any flags you want.
On Windows 7 using Wamp, the option is --skip-lock-tables
Took from this answer
This works when I run it from the mysql cli client but not when I run it through PDO, does anyone know why?
source is a command that allow you to execute list of SQL in mysql CLI,
but PDO is a database driver library (api) that compiled into PHP,
both are in different domain
Details of source :-
https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
http://dev.mysql.com/doc/refman/5.0/en/batch-mode.html
You can either :-
exec("/PATH/mysql < $file");
Or break each line of SQL (inside the file),
execute one-by-one each in PDO
i have mysql file. it has .sql extension. how to run that "mysql" file using php script? how is the script should be?
the mysql file is already on the server, and i want to run this query using php file which is placed on the same place with mysql file...
You could do this
$query=file_get_contents($file);
$mysqli->multi_query($query);
mysqli::multi_query can do the job.
the mysql file is already on the server
dun use PHP ....
Linux ???
install mysql client, and run a shell like
mysql -u root -ppassword -P port_number < any.sql
Windows ???
still need to install mysql client, repeat from above
if you don't have ssh access to the server, and don't have exec right
read/prepare again the mysql file by breaking multiple queries then execute one-by-one