How do I synchronise two MySQL databases using a Cronjob PHP script? - php

i'm developing a webshop with a MySQL database for a client.
This client already invoice management website with a MySQL database.
Now I want to write a php script thats triggered by a cronjob to sync invoice, client and product records.
order record:
id | clientId | status | shipping | reduction
*order_items records:*
id | productId | price |amount | orderId
client record:
id | fname | name | email | ...
Note that only order records with status = 2 should be synchronised, after they have been synchronised, the status should change to 3.
Both databases are using different tables for orders and invoices
What is the best way to do this?

1) Select records
2) Loop over records
3) start transaction (optional)
4) Insert records in db2
5) Update records in db1
6) commit

Are the databases runing on seperate instances of MySQL?
If so...
What is the best way to do this?
Use the same database structure on both systems and use mysql's asynchronous replication.
Failing that, use the federated engine to access tables from one instance on the other, then follow the procedure for same instance below.
If on same MySQL instance....
Make sure you've got an indexed update timestamp on every source table you want to sync and copy the records which are eligible for copying and which have been modified since the last capture.

**you could look into some phpclasses that do this for you http://www.phpclasses.org/search.html?words=mysql+sync&x=0&y=0&go_search=1
Source :- How can i synchronize two database tables with PHP?**

Related

Preventing and delaying concurent mysql insert

I'm trying to create a system for ordering and create a unique serial number to distinguish the order, it's working well until one time there is an order at same time (the difference is just in seconds, about 10 seconds) and then the unique serial number become same (the serial number is increment from the last serial number in db).
I'm creating the id based on some format and it have to be reseted per month so I can't use uniqid().
Do you guys have any idea about this? I read about some db locking but when I tried this solution "Can we lock tables while executing a query in Zend-Db" it's also didn't worked.
---EDIT---
The format is
projectnumber-year-number of order this months
the number of order this months is 4 digits started from 0001 until 9999
after 9999 it will start again from A001 ... A999 B001 ... ZZZZ
and this is the column
| order_id | varchar(17) | utf8_general_ci | NO | PRI |
I hope this make it more clear now :)
Thanks!
Primarily I'd look into using AUTO_INCREMENT primary key - see manual for details.
If that's not possible and you are using InnoDB you should be able to create the order in a Transaction. In your application you can then detect if there were duplicates and re-issue a new ID as needed. Using transaction will ensure that there is no residual data left in the database if your order creation fails.
EDIT based on the additional information:
I'd add an AUTO_INCREMENT primary key and use a separate "OrderName" column for the desired format. That should allow you to do the following, for example:
UPDATE orders o
JOIN (
SELECT
year(o2.dte) y,
month(o2.dte) m,
min(o2.Order_ID) minid
FROM orders o2 GROUP BY y,m) AS t ON (t.m=month(dte) AND t.y=year(dte))
SET o.OrderName=CONCAT('p-n-',year(o.dte),"-",o.Order_ID-t.minid);
id column is int PRIMARY KEY AUTO_INCREMENT and will ensure that the orders are always in correct order and will not require locking. In this example CONCAT will dictate your order number format. You can run this UPDATE in a trigger, if you wish, to ensure that the OrderName is immediately populated. Of course if you run this in a trigger, you don't need to repopulate the whole table.
Seem we must use transaction with Serializable locking. It will prevent read and write from other session until transaction complete.
Please view at here
http://en.wikipedia.org/wiki/Isolation_%28database_systems%29
http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html

Copying records accross multiple databases using PHP

I want to do something which may sound wierd.I have a database for my main application which holds few html templates created using my application.These templates are stored in a traditional RDBMS style.A table for template details and other for page details of the template.
I have a similar application for different purpose on another domain.It has a different database with the same structure as the main app.I want to move the templates from one database to the other,with all columns intact.I cannot export as both have independent content of there own i.e same in structure and differ in content. 1st is the template table and 2nd is the page table
+----+----------+----------+
| id |templatename |
+----+----------+----------+|
| 1 | File A | |
| 2 | File B | |
| 3 | File C |
| 4 | File 123 |
| .. | ....... | ........ |
+----+----------+----------+
+----+----------+----------+
| id | page_name| template_id|(foreign key from above table)
+----+----------+----------+
| 1 | index | 1 |
| 2 | about | 1 |
| 3 | contact| 2 |
| 4 | | |
| .. | ........ | ........ |
+----+----------+------------+
I want to select records from 1st database and insert them to the other.Both are on differnet domains.
I thought of writing a PHP script which will use two DB connections,one to select and the other for insert to the other DB,but I want to know if I can achieve this in any other efficient way using command line or export feature in any way
EDIT: for better understanding
I have two databases A and B both n diff servers.Both have two tables say tbl_site and tbl_pages.Now both are independently updated on their domains via application interface.I have a few templates created in database A stored in tbl_site and tbl_pages as mentioned in the question above.I want the template records to be moved to the database B
You can do this in phpMyAdmin (and other query tools, but you mention PHP so I assume phpAdmin is available for you).
On the first database run a query to select the records that you want to copy to the second server. In the "Query results operations" section of the results screen, choose "Export" and select "SQL" as the format.
This will produce a text file containing SQL INSERT statements with the records from the first database.
Then connect to the second database and run the INSERT statements from the generated file.
As other mentioned you can use phpmyadmin, but if your second database table fields are different, then you can write down a small php script to do that for you. Please follow the following steps.
Note : Consider two databases A and B, and you want to move some data from A to B and both are on different servers.
1) First allow remote access on database A server for the database A. Also get a host, username and password for database A.
2) Now using mysqli_ extension, connect to that database. As you have the host for the other database A server, so you have to use that, not localhost. On most servers, the host is the IP of the other remote server.
3) Query database table and get your results. After you get results, close the database connection.
4) Connect to database B. Please note that in this case, database B host may be localhost. Check your server settings for that.
5) Process the data you got from database A and insert them to database B table(s).
I use this same method to import data from different systems (Drupal to Prestahop, Joomla to a customized system), and it works fine.
I hope this will help
Export just data of db A (to .sql). Or use php script - can then be automated if you need to do it again
Result:
INSERT table_A values(1, 'File A')
....
INSERT table_B values(1, 'index', 1)
....
Be careful now when importing data - if you have ids the same you will get error (keep this in mind). Make any mods to the script to solve these problems (remember if you change an id for table_A you will have to change the foreign key in table_B). Again this is a process which you might be forced to automate.
Run the insert scripts in db B
As my question was a bit different I preffered answering it.Also the above comments are relevant in different scenarios so,I won't say they are totally wrong.
I had to run a script to make the inserts happen based on new ids to the target database.
To make it a bit easy and avoid cross domain request to database,I took a dump of the first database and restored it to the target.
Now I wrote a script to select records from one database and insert them to the other i.e the target.So the ids were taken care of automatically.Only the problem(not a problem actually) was I had to run the script for each record independently.

use case for MongoDB to migrate from MySQL

I'm using PHP and MySQL for social network system
I have MySQL table named member_feed , in this table I save feeds for each member, my table structure is :
|member_feed_id | member_id | content_id | activity_id | active
in this table I have more than 120 million record, and every member has set of record.
I do this queries on this table :
1 - select * from member_feed where memebr_id= $memberId
2- update memebr_feed set active = 1 where member_id = $member_id
3- insert into member_feed (member_id, content_id,activity_id,active values(2,3,59,1) )
for each member daily I have at least new 50 feed by beanstalkd system and I have more than 60000 member.
I currently work to migrate from MySQL to MongoDB and I'm new on MongoDB. so I need to convert this table to collection in MongoDB.
is this use cases good for mongodb and good solution for my system to increase system performance?
Well, In mongodb joins between two collection is not available so, my question is where you will store member,content, activity? i believe these should embedded in the member_feed.
Example:
member_feed Structure:
{_id:1,member:{_id:2,name:"Anuj"},activity:{details:"xyz"},active:false}
Hope that helps!!!
Thanks

Can I use MySQL temporary tables to store search results?

I have a search page written in PHP, and it needs to search in the MySQL database, and the result need to be sortable. This search page will be accessed by many users (>1000 at any time).
However, it is not feasible to sort the search result in MySQL, as it would be very slow.
I'm thinking of storing each search result into a temporary table (not MySQL temporary table), and the table name is stored inside another table for reference like this:
| id | table_name | timeout |
-----------------------------
| 1 | result_1 | 10000 |
| 2 | result_2 | 10000 |
Then I can use the temporary tables to sort any search results whenever needed without the need to reconstruct (with some modification) the query.
Each table will be dropped, according to the specified timeout.
Assuming I cannot modify the structure of existing tables that are used in the query, would this be a good solution or are there better ways? Please advice.
Thanks
There's no need to go to the trouble of storing the results in a persistent database when you just want to cache search results in memory. Do you need indexed access to relational data? If the answer is no, don't store it in a MySQL database.
I know that phpbb (an open source web forum which supports MySQL backends) uses a key-value store to back its search results. If the forum is configured to give you a link to the specific results page (with the search id hash in the URL's query string) then that link will be valid for awhile but eventually be flushed out of the cache, just like you want. It may be overkill to implement a full database abstraction layer if you're set on MySQL though. Anyway:
http://wiki.phpbb.com/Cache
You should just use memcached or something to store the results data, and you can easily retrieve the data and sort it in PHP. Also there are some PHP-specific cache frameworks that minimize the cost of loading and offloading data from the interpreter:
https://en.wikipedia.org/wiki/List_of_PHP_accelerators

Challenge in getting records present in Oracle but missing in MySQL

Our client has a system that is integrated to our Company application..the client system connects to Oracle database and our Company application connects to MySQL database.
The client side have given us a view on Oracle that has a set of 4 columns that resemble columns we have on our MySQL database.See below columns in Oracle database and their counterparts/equivalent in MySQL database.
Our client [ORACLE] | Our Company [MySQL]
[ORACLE] | [MYSQL]
AK_NO | Patient_File_Nr
BIL_NO | Invoice_Nr
PAYMENT_AMT | Amount
VOUCHER_DATE | Insert_Date
I am able to generate records present to each of the databases but then I am not able to figure out a way of getting records missing in either of the databases when doing a comparison check.
How would I write PHP code that connects to both databases and does query for records in MySQL that are not present in Oracle and vice-versa.
If its a once off.. just export the data from one system into the other ( or into a third system if you don't want to make any alterations to either system), them compare using SQL.
If its a continual process, then probably just a process with php to do it, just open a database handle to each system, do your selects on the tables and compare them programmatically.

Categories