How to Insert into another Database Server - mysql

Is it possible to INSERT query to another db server?
Current Db server: 192.168.59.2
Example:
Insert into 192.168.1.1.Testing.Student (id) values (1)

Looks like you need to use MySQL The FEDERATED Storage Engine. Per documentation
The FEDERATED storage engine lets you access data from a remote MySQL
database without using replication or cluster technology. Querying a
local FEDERATED table automatically pulls the data from the remote
(federated) tables. No data is stored on the local tables.
It kind a similar concept like Linked Server in Microsoft SQL Server.

Simply Use Generate Script Feature of the SQL server :
Follow Link

I'm afraid you can't do something like that. Maybe you should look into FEDERATED tables, where you can copy values to a table from one server to another.
You could have something like this on the table, which you're trying to map:
CREATE TABLE federated_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL DEFAULT '',
PRIMARY KEY (id)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://user#your_host:3306/federated/test_table';
This thread might help you.

Thank you for all the answers. I've answered my question by installing a trial version of navicat and export to excel then import to another db server. Thank you again guys!

Related

ORA-01400: Can't insert NULL into ("repository name"."MD_PROJECTS"."ID")

We are migrating a MySQL database to an Oracle 12c database using SQL Developer 4.0.3.16.
After creating a repository we had an error (unable to create repository because it still exists, delete it first.). There was no repository so we just tried again and it worked, repository was created.
Now we are connected to our source database (MySQL), our target database (Oracle) (see picture) and we have another connection with our migrating user (migrepo) to our target database.
Now we are having the following error over and over again..:
(English: ORA-01400: Can't insert NULL into ("MIGREPO"."MD_PROJECTS"."ID"))
Can anyone help us?
Your table MIGREPO.MD_PROJECTS has a column named ID on it which is either a primary key or has a NOT NULL constraint on it (or perhaps both). Something in the code being run is trying to put NULL into this ID column, which the constraint(s) will not allow.
Best of luck.
Solved, I was trying to migrate it as sysdba. Created new user and migrated without errors.
Better try changing your Oracle column as NOT NULL, and while you are converting, select the option as APPEND for what if table exist. It will solve the problem.

MySql Remotely access database

I have a two DB server say Server-1 & Server-2, I have installed MySql yog into it, and the servers are able to connect with each other as I have already provided Grant privileges for the servers.
But, I need to build a query which can extract data from more than one servers like -
Select * from Server1.db.Table1, Server2.db.Table2
Is this possible in mysql, if yes, can you please help me to achieve the same.
Thanks
federated storage engine should help to full fill your requirement.
Follow link to enable Federated storage engine in your server from where you are accessing remote table.
--In Server1:
CREATE DATABASE fed_remote_db
CREATE USER 'fed_remote_user'#'%' IDENTIFIED BY 'fed_remote_password';
GRANT ALL ON fed_remote_db.* TO 'fed_remote_user'#'%';
CREATE TABLE fed_remote_db.fed_table(id INT,NAME VARCHAR(50));
--In Server2:
CREATE TABLE fed_remote_db.Server1_fed_table (
id INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=FEDERATED CONNECTION='mysql://fed_remote_user:fed_remote_password#Server1_IP:3306/fed_remote_db/fed_table';
SELECT *
FROM
fed_remote_db.Server1_fed_table a /* Remember Server1_fed_table table referring to remote server /,
fed_remote_db.server2_fed_table b / server2_fed_table table referring to local server */
WHERE a.id=b.id;
I believe you can convert above example for your requirements.

Federated Table Clarification

In my prior job, I was able to copy data from our production environment in a breeze by using the following statements:
from tablename#UNIXPROD2
INSERT INTO tablename#UNIXTEST2
My current job's databases aren't setup in this fashion.
So, I did some research on MySQL 5.0+ because that's what we are using for one of our customers. And I came across FEDERATED tables, so as I was reading, I found this (here):
As of MySQL 5.0.46, FEDERATED performs bulk-insert handling such that multiple rows are sent to the remote table in a batch. This provides a performance improvement. Also, if the remote table is transactional, it enables the remote storage engine to perform statement rollback properly should an error occur. This capability has the following limitations:
To me, this indicates that (A) I can copy the data from our prod database to our test database; (B) any actions performed on the federated table will also be processed on the source table, which is not what I want to do. I have some scripts that I need to run and I want to run it against actual prod data to make sure it works before I use it in the prod environment.
My question: Is my interpretation correct?
Assuming it is, I've tried:
select * from database.tablename#ipaddress, but received an error message that told me to check the MySQL manual for the version I'm running, which is what I'm going to do after I hit "Post Your Question."
I would appreciate any help in this matter.
EDIT: After further research, I think might be able to do what I need using OUTFILE and INFILE whereby I would use OUTFILE on the prod table(s) and then INFILE those rows on the test table(s). Thoughts?
My answer:
A - correct
B - correct.
You could set the user permission to read-only, but in your situation I would not use federated tables, instead dump the whole db into file and then restore it on the other server. Easiest way - use MySql Workbench.
and some info about federated tables:
You need federated enabled just on server B
You can access a view on A by making a federated table on B
You can do INSERT UPDATE DELETE on federated table
If you need read-only access you can limit the user privileges
BUT! You can't do any aggregate func. on a view which will be federated (ex. COUNT(), MAX(), UNION...) (you can, however it will lag)
Remember to set the KEY's on the federated table you are creating. (or it will lag horr.)
Remember to use ALGORITHM=MERGE on views
Remember to grant acces to USERNAME(from connection string) on server A
example of a federated table on server B:
delimiter $$
CREATE TABLE `schemaName`.`tableName`(
`keyName` VARCHAR(10) NOT NULL,
`key2Name` DATE DEFAULT '2012-01-01',
KEY `keyName` (`keyName`)
)
ENGINE=FEDERATED
DEFAULT CHARSET=utf8
CONNECTION='mysql://USERNAME:PASSWORD#IPADDRESS:PORTNUMBER/baseSchema/baseTable'
$$
And the view on server A:
CREATE
ALGORITHM = MERGE
DEFINER = `ANOTHERUSERNAME`#`%`
SQL SECURITY DEFINER
VIEW `baseSchema`.`baseTable` AS
SELECT
... AS `keyName`,
... AS `key2Name`
FROM
...

I need to join table from other database and sometimes other server

My project has its own database. Also, I use table of users, which is on other database. Two offices have their data on the same server, but third one has its own user table on other server.
So, in lots of queries I need to join either table some_db.users or other_server.some_db.users
What solution would you advise for this scenario?
I use MySQL.
There is Federated tables in MySQL:
The FEDERATED storage engine lets you access data from a remote MySQL
database without using replication or cluster technology. Querying a
local FEDERATED table automatically pulls the data from the remote
(federated) tables. No data is stored on the local tables.
First, you must have a table on the remote server that you want to access by using a FEDERATED table. Suppose that the remote table is in the sakila database and is defined like this:
CREATE TABLE test_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL
PRIMARY KEY (id)
)
ENGINE=MyISAM
DEFAULT CHARSET=latin1;
Next, create a FEDERATED table on the local server for accessing the remote table:
CREATE TABLE federated_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL
PRIMARY KEY (id)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user:fed_user#197.186.1.199:3306/sakila/test_table';
Sample connection strings:
CONNECTION='mysql://username:password#hostname:port/database/tablename'
CONNECTION='mysql://username#hostname/database/tablename'
CONNECTION='mysql://username:password#hostname/database/tablename'
The basic structure of this table should match that of the remote table, except that the ENGINE table option should be FEDERATED.
Execute:
show variables like '%federated%';
to check if FEDERATED storage engine is available on your local server.
The table federated_table in localhost becomes virtual table of test_table in remote server.
Now you can use the JOIN between the tables in a DB in the localhost server. If there is a table called test in your localhost server, and you want to JOIN with the former sakila.test_table which is in the remote server, write a query like the one shown below:
SELECT * FROM `federated_table` JOIN `test`;
The federated_table in the query will actually refer to test_table in remote server.
On enabling FEDERATED Storage Engine
The FEDERATED storage engine is not enabled by default in the running server; to enable FEDERATED, you must start the MySQL server binary using the --federated option.
NOTE:
Optional storage engines require privileges and will fail to load when --skip-grant-tables is specified.
The result the entire db will fail to load and the following error will appear in the logs:
110318 21:37:23 [ERROR] /usr/local/libexec/mysqld: unknown option '--federated'
This in turn means that an upgrade from 5.x needs to be done in two steps if you have federated tables. Once with --skip-grant-tables and without --federated, the once without --skip-grant-tables and with --federated.
Source: The FEDERATED Storage Engine
Please mention the databse also.
In SQLServer you can use Linked sever. http://msdn.microsoft.com/en-us/library/ms188279.aspx
In MySQL, you can join tables from different databases using fully qualified names like
`database_name1`. `table_name1` JOIN `database_name2`.`table_name2`
But i fear, you cant join tables from different servers because for that you need to have two different connections and as per my knowledge there are no fully qualified connection names to be used in the query.
Alternatively, you can create local temporary table(s) on one of the servers and run the query there on. But in this case you will need to transfer data from one server to another. You can use MySQL GUI tool like SQLyog or MySQL admin, to transfer data from one server to another and to synchronize databases on two servers.
Hope it helps....
Federated tables are your solution for tables on other servers. They are very slow though if you perform joins on them.
If you just want to read data from another database on the same server you can use a view. This way you have all tables virtually in one database and you have to open only one connection in your application.
CREATE
VIEW `my_db`.`table_name`
AS
(SELECT * FROM `other_db`.`table_name`);

Proper syntax for MySQL 5.0.x FEDERATED table creation?

So I'm trying to create a federated table using the syntax from the docs. Following this, I've created a table like so:
CREATE TABLE `federated_table` (
`table_uid` int(10) unsigned not null auto_increment,
...,
PRIMARY KEY (`table_uid`)
) ENGINE=FEDERATED DEFAULT CHARSET=latin1 CONNECTION='mysql://user:password#host.name:3306/';
Every time I do this, I get the error:
ERROR 1432 (HY000): Can't create federated table. The data source connection string 'mysql://user:password#host.name:3306/' is not in the correct format
I've looked at the docs, and I believe that I'm following the docs in this. What is the proper syntax for this connection string?
I wasn't following the docs after all. I neglected to add the remote database and table into the connection string. The proper connection string would have been:
mysql://user:password#host.name:3306/remote_db/table
Also make sure your /etc/my.cnf has
[mysqld]
federated
Then restart the mysql service. It may not be enabled by default.