MySql table replication on the same server - mysql

I have two MySql tables on the same server ( for example DB1.customer and DB2.customer ). I want to replicate the content of DB1.customer in DB2.customer and at every INSERT, DELETE or UPDATE of DB1.customer I wanto to sync with DB2.customer. Is possible this with replication ? If yes what's the configuration ? Have I to specify two different server-id ? I've read that in this case I have to install two instance of MySql in different directory and then use two different server-id for each installation, is this true ? Is there any other way to achieve what i'm looking for without install another instance of MySql ?
Thanks a lot for your tips.

I've resolved with triggers ( like #BK435 suggest... ). This is the code I used from DB1 table:
DELIMETER $$
CREATE TRIGGER ins_new AFTER INSERT ON customer FOR EACH ROW
BEGIN
INSERT INTO DB2.customer ( 'id', 'name', 'email','passwd' )
VALUES ( NEW.'id', NEW.'name', NEW.'email', NEW.'passwd' )
END;
$$
Is important to change delimeter if ';' is set as default delimeter.

Related

#1062 - Duplicate entry '1' for key 'PRIMARY'

I am at a complete loss here. I have two databases. One on my localhost site that I use for development and one on my remote site that I use for my live (production) site. I manage both of them through phpMyadmin. As I have been doing for months now, when I need to update the live site, I dump the related database and import the database from my localhost site.
Now, no matter what I try, I keep getting this error:
Error
SQL query:
--
-- Dumping data for table `oc_address_type`
--
INSERT INTO `oc_address_type` ( `address_type_id` , `address_type_name` )
VALUES ( 1, 'Billing' ) , ( 2, 'Shipping' ) ;
MySQL said: Documentation
#1062 - Duplicate entry '1' for key 'PRIMARY'
I tried creating a new blank database on my localhost and importing into that but same results. I have validated all of the tables and indexes and cannot find anything wrong there.
Any suggestions please as I am completely down until this gets resolved.
By the way, I am completely dropping all tables and importing structure and data. This has always worked until today.
you need to dump with the drop statements. The table exists and has data already and your trying to insert more which is identical. Im not 100% sure on phpmyadmin but the dumps will have an option for "add drop table" statements
Dump your database on localhost with "mysqldump --insert-ignore ..." then try to import with phpmyadmin on your live machine.
Or try to connect to your live database with command line tools (configure your database to be able to connect from other hosts than "localhost" first!)
Then you can try following:
$ mysql -f -p < yourdump.sql
with -f "force" you can ignore errors during importing. It's the same as adding "--force" parameter to "mysqlimport".
The problem is related with your file - you are trying to create a DB using a copy - at the top of your file you will find something like this:
CREATE DATABASE IF NOT EXISTS *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE *THE_NAME_OF_YOUR_DB*;
and I'm sure that you already have a DB with this name - IN THE SAME SERVER - please check, because you are trying to overwrite!! Just change the name OR (better) ERASE THIS LINE!
For me the foreign_key_checks and truncate table options was useful.
SET foreign_key_checks = 0;
TRUNCATE `oc_address_type`;
SET foreign_key_checks = 1;
Run the above sql script, and after the import.
I had this same issue, my problem was I had a primary key column called unique_id and when you try to add two of the same value in that primary keyed column, it comes back with the error below.
A primary key column's data is all suppose to be different, so that bottom 1 I changed to 3 and the error went away.
Your MySql is not corrupt, like previous answers and comments.
you need to delete any previous tables that you are over-writing. if you are doing a complete restore of all tables, delete all existing tables.
I have met the same problem, I drop the table and rebuilt the database, then the problem solved.

Sync two MySQL tables

Have two separate databases. The master that holds the user information(username, password, address etc.). The slave database only has one table where the user name and password. I would like to happen is then an new user on the master db i created that the username and password is also added to the slave db.
You can do this with either a TRIGGER or STORED PROCEDURE.
In your case i guess you could use something like this (not tested):
CREATE TRIGGER `user_update` AFTER INSERT ON `User`
FOR EACH ROW
BEGIN
INSERT INTO `mydb`.`UserLogin` (`id`, `UserName`, `Pass`)
VALUES (new.UserId, new.UserName, new.Password);
END$$
We face a similar situation. We use Percona Toolkit's pt-table-sync tool. It's rather simple to use.

Can a VIEW in MySQL read from one table and store into another?

Does MySQL have anything like aufs? I want to use my production tables read-only and store changes into another table or database if possible.
Thank you!!
Does MySQL have anything like aufs?
No
Option1 - Use replication
You can put the (read-only) production database on one server and the change database on another server (can be on the same machine).
If you set the change database as a slave of the production database, all changes that happen (from some other source perhaps) will be replicated to your change database.
Changes in the change database will not be passed on to the production database.
The fact that the change database is not in sync with the production database may cause problems with the (one way) synchronization but you'd have to experiment with that.
See: http://dev.mysql.com/doc/refman/5.0/en/replication.html
Option 2 - dirty hack with triggers
You can put a trigger on your production table and have the trigger diverge the data to somewhere else.
warning this is a filthy hack and not recommend use of triggers at all.
DELIMITER $$
CREATE TRIGGER bu_prod_table1_each BEFORE UPDATE ON prod_table1 FOR EACH ROW
BEGIN
UPDATE change_table1 c SET c.field1 = NEW.field1, c.field2 = NEW.field2
WHERE c.id = OLD.id;
IF NEW.id <> OLD.id THEN
UPDATE change_table1 SET c.id = NEW.id WHERE c.id = OLD.id;
END IF;
/*reverse the changes in the production table*/
SET NEW.id = OLD.id;
SET NEW.field1 = OLD.field;
.....
END $$
DELIMITER ;
You'd have to create these triggers for UPDATE, DELETE, and INSERT.
And you'd have to put triggers on each and every table in production.
In other SQL implementations (e.g. SQL Server), an INSTEAD OF trigger on a VIEW can achieve the goal of reading from one table and writing to another. However, mySQL does not to my knowledge support INSTEAD OF triggers.

temporary tables within stored procedures on slave servers with readonly set

We have set up a replication scheme master/slave and we've had problems lately because some users wrote directly on the slave instead of the master, making the whole setup inconsistent.
To prevent these problems from happening again, we've decided to remove the insert, delete, update, etc... rights from the users accessing the slave. Problems is that some stored procedure (for reading) require temporary tables.
I read that changing the global variable read_only to true would do what I want and allow the stored procedures to work correctly ( http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_read_only ) but I keep getting the error :
The MySQL server is running with the --read-only option so it cannot
execute this statement (1290)
The stored procedure that I used (for testing purpose) is this one :
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_readonly` $$
CREATE DEFINER=`dbuser`#`%` PROCEDURE `test_readonly`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS temp
(
`BT_INDEX` int(11),
`BT_DESC` VARCHAR(10)
);
INSERT INTO temp (BT_INDEX, BT_DESC) VALUES (222,'walou'), (111,'bidouille');
DROP TABLE temp;
END $$
DELIMITER ;
The create temporary table and the drop table work fine with the readonly flag - if I comment the INSERT line, it runs fine- but whenever I want to insert or delete from that temporary table, I get the error message.
I use Mysql 5.1.29-rc. My default storage engine is InnoDB.
Thanks in advance, this problem is really driving me crazy.
There seems to be a bug opened about this for the 6.0 beta:
http://bugs.mysql.com/bug.php?id=33669
[3 Jan 2008 19:26] Philip Stoev
Description: When the server is
started with --read-only, updates to
Falcon temporary tables are not
allowed.
You might want to add your findings there.

How to configure binlog to get User-Information?

I need to get an auditrail in mysql; is there a way to configure the binary log to get not only the changes, also the user, (connection) who made this change? Or do I have to use mySQL Proxy?
TIA
Peter
I don't think its possible to have the binlog show connection info. My approach is to set triggers in the database that log to audit tables. For example, here is one from work:
CREATE TRIGGER whatever_audit_INSERT
AFTER INSERT ON whatever FOR EACH ROW
BEGIN
INSERT INTO whatever_audit(
audit_when_start, audit_who_start, col1, col2
) VALUES (
now(), #app_user, new.col1, new.col2
)
END
That's from memory; hopefully I got the syntax right...