How to configure binlog to get User-Information? - mysql

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...

Related

MySql table replication on the same server

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.

How To Use gearman client in MySQL Trigger implementation

I have set up gearman udf for database and trying to send a gearman job from mysql query. Normally it is just working fine. as conventional call from mysql query is as follows:
SELECT gman_do_background("eventName", "data");
Now the problem is happening when I am trying to give this call from a mysql trigger implementation. An error is showing up as follows:
MySQL said: #1415 - Not allowed to return a result set from a trigger
So, basically as you can see, either
There is some other way for the gearman call or
Somehow I have make a fake update call with the gearman select call inside there.
I am trying to write the Trigger which is pretty much simple as below:
BEGIN
SELECT gman_do_background("eventName", "#data") FROM
(
SELECT #data := CONCAT(a,',',b,',',c) FROM mytablename WHERE status = 1
)
END
but as yo can see, because of the 'SELECT' operation, its not saving and throwing the above error.
Can anyone please help me whether there is any alternate gearman call type(other than 'select') or is there any way to write update query that doesn't affect any mysql table/column? Thanks in advance.
First of all doing any non-transactional operations in a trigger are wrong. In case of a rollback you won't be able to undo calls to your udf function. So I'd suggest reconsider using triggers for this type of calls.
Highly recommended reading:
The Trouble with Triggers
Now as you figured out SELECTs on their own are prohibited in triggers since there is no client to return the resultset to.
But you can legitimately use INSERT INTO ... SELECT FROM ... syntax. You can create an auxiliary table with BLACKHOLE engine if it is enabled in your MySQL instance. Anything you write to blackhole goes to /dev/null.
CREATE TABLE dev_null
(
value VARCHAR(255) -- adjust data type as needed
) ENGINE=BLACKHOLE;
Then in your trigger
INSERT INTO dev_null
SELECT gman_do_background('eventName', #data)
FROM
(
...
)
Here is SQLFiddle demo
Maybe this link http://codesamplez.com/development/gearman-from-mysql-udf helps (tip 2)

SQL Trigger errors

I'm trying to create a trigger like so:
CREATE TRIGGER `table_insert` AFTER INSERT ON `user`
FOR EACH
ROW BEGIN
INSERT INTO
VALUES log (username,action)
NEW.name, 'successful addition'
);
END ;
I'm just looking to experiment with triggers but i get many errors no matter what I do, when i Try and work it I might get an error like so:
SUPER privilege needed for this operation
Check syntax
Could some1 who has any knowledge please show me how to add a trigger?
I am using PHPmyAdmin to work on my mysql database hosted on a blacknight server.
Is new.name --> name correct column? or username?
IF not:
Insert into log(username,action)
values(
new.name,
'successful addition'
)

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.

SQL Server : track table inserts

I have a table which get new data inserted every minute or so. According to the source code I have here, it is only done in one class which is not used anymore.
Is there any way to trace the inserts? What I mean is to see which queries they were inserted by, who sent those queries etc. As much info as possible.
I have tried several ways myself (e.g.sp_who2 'Active' stored procedure) without any success. I also have access to the machine running the SQL server and to the transaction backup files (.trn files) but have no idea how to open those files.
Add trigger to the table which follows inserts and insert to other table these variables:
getdate(),
host_name(),
App_Name(),
suser_sname()
Seems to me that this is enough
The trigger looks like this:
CREATE TRIGGER YourTrigger On YourTable
AFTER INSERT
AS
SET NOCOUNT ON;
INSERT logtable
SELECT APP_NAME(), HOST_NAME(), SUSER_SNAME(), GETDATE(), * FROM INSERTED
GO
OR
you can use Sql Server Profiler for catching the queries - it may be more flexible
You may use sp_depends like this:
sp_depends tablename
This only states information in the same database but it might say what you need!