Looking for a MySQL query command to insert all the queries in local .sql file to the MySQL database on a remote server - insert

Is there a MySQL query command to upload/insert all the queries in a .sql file (generated from mysqldump) on a local server to a mysql database on a remote server?
I'd like to try and do this all within MySQL queries from within an application and avoid issuing command-line mysql commands because I think that there would be a bit more overhead in parsing the output in that way.
I'm looking for something like, e.g. in Perl:
my $hostname = "remote_server_address";
my $dsn = "DBI:mysql:dbname:$hostname";
my $user = "user";
my $password = "password";
my $dbh= DBI->connect($remote)dsn, $user, $pw) );
my $myquery = "SPECIAL_INSERT_QUERYCOMMAND my_local_mysql_query_file.sql";
my $execute = $dbh->prepare($myquery);
$execute->execute;
Update: Additional requirements: Is there any "flow-control and resilience" whereby any connection issues between the local and remote server are handled so that the entire set of queries get transfered. And would there be a limit to the size of the file to be transferred? (I hope not).

I'm not sure why you couldn't use the command line tool mysql
cat my_local_mysql_query_file.sql | mysql -uuser -ppassword dbname
or on windows
mysql -uuser -ppassword dbname < my_local_mysql_query_file.sql

Related

How to format oracle connection string and output query to file

I am trying to connect to a oracle database, query it, and send results to a txt file. When I run my statement, this shows in the .txt file:
In reality, it should be values from my sql script.
Here is the string i am running:
sql_file1=Cb.sql
sqlplus -s "username/pwd#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=my_host)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=my_ser_name))))" #sql/$sql_file1 > /home/path/to/my/files/'cb.txt'
Any reasons as to why my 'cb.txt' file shows the screenshot from above instead of any date from the query inside my sql file?
You have extra ) in your connection string:
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=my_host)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=my_ser_name))))
should be
sqlplus -s "username/pwd#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=my_host)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=my_ser_name)))" #sql/$sql_file1 > /home/path/to/my/files/cb.txt
But even easier to use EZConnect string:
sqlplus -s "username/pwd#//my_host:1521/my_ser_name" #sql/$sql_file1

Backup & Restore PostgreSQL database and setup localhost environment with laravel in windows 7

1) open C:\Program Files\PostgreSQL\12\data\pg_hba.conf
change:
host all all ::1/128 md5
to
host all all ::1/128 trust
2)open pgAdmin & create a localhost server with username postgres and password will empty
/* For taking a backup or restore a dump of existing database name */
open cmd line and go to C:\Program Files\PostgreSQL\12\bin and press enter
and type below command as required
Take Backup :
pg_dump.exe -U postgres -d dbname -f D:\Backup\
or direct take backup using pgAdmin backup option and store in D:\Backup\<backup-file-name>
hint: backup file should be tar or dump type
Restore Backup : pg_restore -U postgres -d dbname -1 D:\Backup\
3) In laravel code folder open .env file and add DB_SSLMODE=disable
4) in laravel code folder open config/database.php and for 'pgsql' array
replace
'sslmode'=> 'require',
to
'sslmode' => env('DB_SSLMODE','require'),
How to Backup PostgreSql DB In Laravel
install laravel package using composer.
composer require spatie/laravel-backup
insert the following line to your backup controller.
use Spatie\DbDumper\Databases\PostgreSql;
write the following code in your backup controller.
date_default_timezone_set('EST');
try {
$this->info('The backup has been started');
$backup_name = 'backup-' . date('c') . '.sql';
$backup_path = 'app/backups/' . $backup_name;
PostgreSql::create()
->setDbName(env('DB_DATABASE'))
->setUserName(env('DB_USERNAME'))
->setPassword(env('DB_PASSWORD'))
->dumpToFile($backup_path);
$this->info('The backup has been proceed successfully.');
} catch (ProcessFailedException $exception) {
logger()->error('Backup exception', compact('exception'));
$this->error('The backup process has been failed.');
}

db2 drop few database as once

recently I started to work with db2, and created few databases.
To drop a single DB I should use db2 drop db demoDB, is there a way to drop all DBs at once?
Thanks
Taking into account the previous answer, this set of lines do the same without creating a script.
db2 list db directory | tail -n +6 | sed 'N;N;N;N;N;N;N;N;N;N;N;s/\n/ /g' | awk '$28 = /Indirect/ {print "db2 drop database "$7}' | source /dev/stdin
This filters the local databases, and executes the generated output.
(Only works in English environment)
first , i don't think there is any db2 nature way to do that. But I usually do the following thing. At start, the way to see all the databases on your instance is one of the following:
db2 list db directory
db2 list active active databases
Depends on your need ( all DBs or just the active DBs)
I'm sure there is more DBs lists you can get.(at DB2 user guide)
The way I usually drop all my DBs is by using shell script:
1. create new script by using 'vi db2_drop_all.sh' or some other way you want.
2. paste the code:
#!/bin/bash -x
for db_name in $(db2 list db directory | grep Database | \
grep name | cut -d= -f2); do
db2 drop db $db_name || true
done
exit 0
3. save changes
4. and just run the script (after you switched to your instance of course ) sh db2_drop_all.sh
Notice that in step 2 you can change the list of DBs as you wish. ( for example to db2 list active databases)
Hope it helped you. :)

Attempting to connect to an oracle database from shell script

I am trying to connect to an oracle database from a shell script ( I am a new user ) . The script will then pass a query and transfer the result to a variable called canadacount. I have written the code but it does not work
#this script will attempt to connect to a remote database CFQ143 with user ID 'userid' and password 'password'.
#After loggin in it will read data from the PLATFORMSPECIFIC table.
#We can pass a query 'select count (platform) from platformspecific where platform='CANADA';
#The result from this query will be passed to a variable called canadacount which we can then echo back to the user.
canadacount='$ORACLE_HOME/bin/sqlplus -s /nolog<<EOF
connect userid/passsword#CFQ143:1521:CFQ143
set pages 0 feed off
select count (platform) from platformspecific where platform='CANADA';
exit
EOF'
echo $canadacount
The answer is :
I changed the connect line to the following:
connect userid/passsword#CFQ143

Password protecting an existing SQL Server Compact 4 database file

Can someone tell me how to password protect an existing SQL Server CE 4 file?
In the SqlCeEngine.Compact method, specify a password. Or you can use SqlCeCmd from a command line:
sqlcecmd40
-d "Data Source=C:\test.sdf;"
-z "Data Source=;Password=secret"

Resources