Connect to Mysql server to run tasks on two databases - mysql

I am writing a script to perform tasks between two MySql databases on the same server, i.e truncate tables on one db and import table rows from another db to this one.
The user who is doing the tasks has full permissions on both databases.
How do I connect to both databases from the command line?
Thanks in advance for any help.

You can use mysqlcommand line utility with the proper parameters:
mysql -u root -h your_host -p your_db
Here root is the privileged user and your_db is the database which is in use by default. You can always switch between databases by typing use another_db command from mysqlconsole.
Also note that you do not have to select dabase (use db_name) in order to execute query on it. You can for example write a query something like this:
SELECT a.id, b.title FROM db1.table1 AS a
LEFT JOIN db2.table AS b ON b.id = a.foreign_id

erm, well I would suggest you open up two terminal windows. the command to connect is:
mysql -u DBUSERNAME -h DBSERVER -p DBNAME
assuming you have mysql installed, which for ubuntu would be: sudo apt-get install mysql

Related

Mysql events using cpanel cronjob

We have a queueing system that was developed by our previous developer, and the truncate command was manually executed to the mysql query. We cant use event scheduler on cpanel so the best option we have is to use cronjob. however, we have no idea on how to execute linux command.
Can someone help me to make a linux query for this?
TRUNCATE TABLE counter_logs_vxphl;
Try to execute the command using the following command, replace {USER} with the username and {PASSWORD} with the password, if you want to specify the host just add -h {HOSTNAME}
mysql -u {USER} -p{PASSWORD} -e "TRUNCATE TABLE counter_logs_vxphl;" > mysql-truncate.log

Import MySQL Query from one server straight to another?

I have two Linux (Debian) servers with MySQL (5.5) on them.
How can I run a query on one and import it directly into another one. I was thinking of something like the below, but can't figure out the last bit.
mysql -h1.2.3.4 -P3306 -uxxxx -pxxxx -e "SELECT id FROM db1.table1 limit 10" | mysql -h5.6.7.8 -P3306 -uxxxx -pxxxx -e "INSERT INTO db2.table2 (id) VALUES ????"
Is this actually possible, or do I need to find some other way of doing it?
maybe you can try to generate a db script of your existing DB then execute this on the target destination DB command line. Dont forget to copy the generated sql script into the destination if it is a seperate machine.
mysql -u <username> -p <databasename> < <scriptFilname.sql>

(Ruby) How to dump a database?

I want to do a database dump through Ruby scripting, but I didn't find any class or script for do that.
Ideally the dump should work for MySQL, PostgreSQL, SQLite, etc. (at least MySql and Pg). I tried with DBI but I can't.
Other way is doing table for table ... D:
Edit 1:
It's only for backup, no restore.
For now I have troubles with hostings, because I need to ask for permits to my IP, so I will try with SSH.
How about using exec in your script to run the mysqldump app?
exec 'mysqlinstalldir/bin/mysqldump -u username -ppassword --databases databasename'
You should be able to do the same with Postgresql and pg_dump
you can use your system tools for taking dump of database,run this script in your code may be a rake task
system "mysqldump database_name table1 table2 -u root -p password > path/to/dump/file "

How do I select a MySQL database through CLI?

I've managed to get into MySQL using the command line terminal, but when I tried to enter some SQL, it said 'no database selected'
how do I select a database? my database name is: photogallery
What code do I use to select it?
Use USE. This will enable you to select the database.
USE photogallery;
12.8.4: USE Syntax
You can also specify the database you want when connecting:
$ mysql -u user -p photogallery
While invoking the mysql CLI, you can specify the database name through the -D option. From mysql --help:
-D, --database=name Database to use.
I use this command:
mysql -h <db_host> -u <user> -D <db_name> -p
Switch to a database.
mysql> use [db name];
MySQL Commands
Hope this helps.
use [YOUR_DB_NAME];
Alternatively, you can give the "full location" to the database in your queries a la:
SELECT photo_id FROM [my database name].photogallery;
If using one more often than others, use USE. Even if you do, you can still use the database.table syntax.
Use the following steps to select the database:
mysql -u username -p
it will prompt for password, Please enter password.
Now list all the databases
show databases;
select the database which you want to select using the command:
use databaseName;
select data from any table:
select * from tableName limit 10;
You can select your database using the command use photogallery;
Thanks !
USE database_name;
eg. if your database's name is gregs_list, then it will be like this >>
USE gregs_list;

Is there a way to copy all the data in a mysql database to another? (phpmyadmin)

I want to copy all the tables, fields, and data from my local server mysql to my hosting sites mysql. Is there a way to copy all the data? (It's only 26kb, very small)
In phpMyAdmin, just export a dump (using the export) tab and re-import it on the other server using the sql tab.
Make sure you compare the results, I have had phpMyAdmin screw up the import more than once.
If you have shell access to both servers, a combination of
mysqldump -u username -p databasename > dump.sql
and a
mysql -u username -p databasename < dump.sql
on the target server is the much more fast and reliable alternative in my experience.
Have a look at
Copying MySQL Databases to Another Machine
Copy MySQL database from one server to another remote server
Please follow the following steps:
Create the target database using MySQLAdmin or your preferred method. In this example, db2 is the target database, where the source database db1 will be copied.
Execute the following statement on a command line:
mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server]
-u [user] -p[password] db2
Note: There is NO space between -p and [password]
I copied this from Copy/duplicate database without using mysqldump.
It works fine. Please ensure that you are not inside mysql while running this command.
If you have the same version of mysql on both systems (or versions with compatible db file sytsem), you may just copy the data files directly. Usually files are kept in /var/lib/mysql/ on unix systems.