I am planning to increase My site performance by adding another MySQL server beside the current one because the current server is too busy.
Is it possible to scale PHP application with MySQL replication without PHP code change? I means all quires will be sent to the master and the master will distribute the load between itself and the slave.
Is there any easy way to send all write quires to the master and distribute read quires between the master and slave?
I think you need to put a load balancer / proxy between your db servers and clients (your code). Example solutions are:
HAProxy: http://haproxy.1wt.eu/
MySQL Proxy: https://launchpad.net/mysql-proxy
If you don't want to do the "load balancing" manually, you might want to look into MySQL Proxy.
I think you should also optimize your application's code (PHP) and then you should optimize your architecture.
First of all you can check your MYSQL queries. Mysql slow query log can help you. If you have a connection issues (MYSQL server has gone away or too many connections etc) you should manage your application's connection pooling mechanism.
And other steps and also your answer is (I think), you can set up MYSQL master-master replication. When you set replication clearly, you can put a load balancer (HAProxy) front of your replication.
You have 2 nodes for mysql (server A and server B, both of them master server)
You can configure HAProxy with server A is master and server B is backup server. Your all MYSQL operations comes server A via HAProxy and your data is automaticly sync with server B.
When server A is down, HAProxy sends all queries server B automaticly.
Also you can configure HAProxy with server A is all insert queries and server B is for all read queries.
All this cases your code should connect MYSQL via HAProxy
Related
I am developing a web application in CodeIgniter and MySQL.
I need to set up a function to update data in MySQL in my local server when there is no internet connection and later switch to live server when the connections are available, restore the locally saved data into live database as well.
(There may be clients 1) at remote location who have poor internet connectivity which I need this idea for and 2) the other who can add data directly to the live server).
Is there any mechanism to get this done?
Mysql Database Replication
Replication enables data from one MySQL database server (the master) to be replicated to one or more MySQL database servers (the slaves). Replication is asynchronous - slaves need not be connected permanently to receive updates from the master. This means that updates can occur over long-distance connections and even over temporary or intermittent connections such as a dial-up service. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.
Situation:
Php application with mysql database running on 2 sites
online -static ip X.X.X.X
localhost (not online most of time and dynamic ip)
application traffic is usually low <10 users.
what i need is that whenever a change is done to the online database, this change is pushed to localhost -if its online or when ever its available- and vise versa (any changes done locally is uploaded online to database when ever there is online connection).
is it possible to setup such replication by mysql ? or do i need to write a custom PHP that ping master server and once its available
thanks very much :).
Yes you can do this with replication. Just pick which server you want to be the master and have the second one send all of its changes to the main one then the main one could send its changes back.
Replication can be a bit daunting to set up but once its up and running its grate. http://dev.mysql.com/doc/refman/5.0/en/replication-howto.html
Let's first analyze your question:
The problem of accessing MySQL with a dynamic ip.
This is very easy. Once you installed MySQL on a server with an ever-changing IP, what you can do is go to NO-IP, DynDNS or any other Dynamic DNS service and register for free with them. Once you've registered with them, you would get a client for your operating system. Install that and then you can access your MySQL server using a domain name.
Example:
Instead of having to access your server at 127.0.0.1, you can access it as mysql-server.easynet.net etc.
Now the second and albeit complex part of your question, how to do available and lazy replication.
This is relatively a bit more complex than the previous step. But, what actually happens is that you have to choose a scheme of replication. Basically what you are looking for here is MASTER-MASTER replication since you have a possibility of changes happening at both the MySQL servers. Thus the updates need to be bi-directional, that's what this scheme of replication does. How to do it? Well, I am providing the links which I've found easier to follow:
Master-Master Replication
Step-by-step MySQL Master Replication
I hope that would ease your plight and answer your question!
Cheers!
Sure, you can
You need to setup both MySQL servers as Master and Slave at the same time.
Configure the online server as Master, and the localhost server as slave, and once replication is OK.
Configure the localhost as Master and the online server as slave.
I already did that on two servers.
About the dynamic IP on the local host, simply you can use any dynamic IP service like: no-ip, and use the dns name instead of the IP.
Here's a post i've written (in french, but you can get the configuration snippets from it) for setting up a MASTER-MASTER replication with a load balancer (mysql proxy) for balancing SQL queries between both nodes.
We have our database servers separate from our webserver. The database servers are replicated (we know there is overhead here). Even with replication turned off however, performance for large number of queries in a PHP script is 4 times slower than our staging server that has the db and apache on the same machine. I realize that network latency and other issues with a network mean that there is no way they will be equal, but our productions servers are exponentially more powerful and our production network is all on gigabit switches. We have tuned MYSQL as best as we can but the performance marker is still at 4x slower. We are running over nginx with Apache proxies and replicated MYSQL dbs. UCarp is also running. What are some suggestions for areas to look for improving the performance? I would be happy with twice as slow on production.
It's difficult to do much more than stab in the dark given your description, but here's some starting points to try independently, which will hopefully narrow down the cause:
Move your staging DB to another host
Add your staging host to the production pool and remove the others
Profile your PHP script to ensure it's the queries causing the delay
Use an individual MySQL server rather than via your load balancer
Measure a single query to the production pool and the staging server from the MySQL client
Run netperf between your web server and your DB cluster
Profile the web server with [gb]prof
Profile a MySQL server receiving the query with [gb]prof
If none of these illuminate anything other than the expected degradation due to the remote host, then please provide a reproducible test case and your full MySQL config (with sensitive data redacted.) That will help someone more skilled in MySQL assist you ;)
Not every web request on a web site will (if properly designed) need a mysql connection. Most likely, if you are requiring a connection on every http request, your application will not scale and will start having issues very quickly.
Do more caching at app. server to request mysql less often. E.g. use
memcache.
Try to use persistent connections from application to your mysql servers.
Use mysql data compression.
Minify data (limit your selects, use column names instead of "*" in select statements)
Shamanic tuning:
Make sure, that nothing slows down network at mysql servers: big firewall rulesets, network filters, etc.
Add another (client inaccesible) network interface for app. server
and mysql server.
Tune network connection between app. server and mysql. Sometimes you
can win several ms by creating hardcoded network routes.
Don't think any of above would help - if network connection is slow, nothing of above will significantly speed it up.
I have a local intranet application which runs off a basic WAMP server in our offices. Every morning, one of our team members manually syncs our internal mysql db with our external mysql db (where our online enrollments occur). If a change is made during the day on the intranet application, it is not reflected on the external db until the following day.
I am wondering if it is possible to (essentially) tunnel to an external mysql connection from say a wamp or xampp server from within our offices and work in 'real-time'.
Anybody had any luck or advice?
Yes
Replication enables data from one MySQL database server (the master) to be replicated to one or more MySQL database servers (the slaves). Replication is asynchronous - slaves need not to connected permanently to receive updates from the master. This means that updates can occur over long-distance connections and even over temporary or intermittent connections such as a dial-up service. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.
If you use the external server directly, performance is likely to suffer. A Gigabit LAN might be a thousand times faster than your Internet connection - particularly the upload speed of an ADSL connection.
Just make your internal application use the database from the external one. You may need to add permission to the external server to allow connections from your internal server IP, but otherwise this is just like having a webserver and sperate db server that need to access each other.
Can't really tell you how to do this here - it all depends on your specific configuration, something that I would thing is a little complicated (and too specialized) to figure out on SO.
How it is possible to update localhost MySql database with server mysql database at particular interval. Web application is made in PHP and database should be in MySql.
Suppose I have a Database at Hosting site Now i want to update my database at regular interval with Local Host Database at my Local Computer.Please suggest me ??
Sounds like you need to use MySQL's replication feature. Here's a walkthrough and here's the manual for reference.
It's worth noting that replication over the internet (rather than a local network) can be slow and you can end up with a slave that's minutes, even hours behind a busy master. Another option could be to periodically mysqldump the master database and copy it back to your local machine, although if your master is very busy you'll lock databases and hang queries, and if it's very large this process will take a long time.