MySQL - Double connection on two Databases? - php

This is just a quick question, I couldn't find it on the internet, but I did wanted to have a confirmation.
Say that if I, using PHP, want to connect to (for both the same) MySQL server, but I want to use two databases, not tables. Because you can connect to only one MySQL-database in the PHP mysqli_connect() command, will, connecting to two databases, though on the same server, use two connections.
Because then it'll probably go fast with the max_connections, right?
P.S. What was the SQL-command again to see the mac_connections value again? I thought it started with GET GLOBAL ... or something, but I might be wrong.
I do not have access to the server, only to (limited) FTP files (it's a bit irritating).

SHOW VARIABLES LIKE '%max_connections%'

If you have both databases in same server you can connect to the server and select one database. You can query from any other databases in the server as long as you have permission. Use the syntax databasename.tablename.columnname in your queries.
If your databases are in different server you may have to do little more work on the server side. you can use the The FEDERATED Storage Engine

I already found it, though I ran into it by pure accident mysqli_select_db().

Related

Does a PHP to MySQL connection need to be local?

Okay, I know this may be a very noobish question, so forgive me, but right now I have XAMPP and I'm running a local Apache server on my personal computer to test PHP code. I have setup a test database through phpmyadmin on a webhost (Hostgator), but it's looking like if I want to connect to that database I need to have the PHP file that I am editing on the same server because any of the tutorials I read tell me to use locoalhost for the servername requirement when using MySQLi or PDO.
Is this because you 'can't' connect to a database on a separate server? Or because it's just not common because there is a better way to do things? (I've seen hints on being able to download MySQL and phpmyadmin onto my PC, and then importing and exporting tables, but what I've seen hasn't been clear on if that's what I need to do for this or not.)
Thank you!
No, it's very common practice to use different machines for web and mysql. There could be a few issues (not familiar with Hostgator but I've dealt with similiar). One is likely firewall. Anything external to the Hostgator network will not have access.
if I want to connect to that database I need to have the PHP file that
I am editing on the same server because any of the tutorials I read
tell me to use locoalhost for the servername requirement when using
MySQLi or PDO.
I'd find better tutorials. It's good practice to separate your MySQL Server away from your web server.
It is possible to connect to a database server running on another machine provided that the server machine has the appropriate ports open in its firewall and that there is a route between both machines.
Problem solved! I had to go into the Remote MySQL and allow the IP address. Again, maybe a noobish thing, but I had no idea that was something you needed to do. Learned from this though! Thank you everyone!
You can do it, and I see that you figured it out.
However exposing your database to the public internet is never a good idea, so for security purposes it is turned off by default.
If the reason is just for testing your code then it is better to setup your development environment with a local mysql server, this way you don't mess with production database.

How Does the PHP Mongos Driver Determine Which Server to Connect To

Say you have multiple Mongo shards in a production environment, so each of them are replica sets. You have three different Mongos instances running for connecting to these replica sets, so you're doing something like this:
new MongoClient("mongodb://mongos1.example.com:27017,mongos2.example.com:27017,mongos3.example.com:27017");
What scheme does the PHP Mongos driver use to determine which of these it's going to connect to? Doing a search for this, I've been able to find relatively little information, and the few things I have come across tend to contradict each other, some saying it picks the first one to respond, and others saying it picks the first one you enter in your code.
Does anyone know?
This is for PHP 1.3.4.
As Sammaye stated, the MongoClient connects to the "nearest" mongos first, where "nearest" is determined by latency. If you're interested, you can take a look at manager.c and read_preference.c in the php driver.

Usage of Mysql in offline internet state

I'm using a self-made customer system in PHP running with a local mySQL Database.
Now i have a second computer on a different location which has to use this Database too. So i gave this mysql Database on a Server reachable through internet.
My problem is now, that the first one has often problems with the internet connection and then the program will not work. But it has to work every time!
Now i do not know how i should handle this problem?
A local Database and one in the internet, but how should i merge them?
Should i make a local DB per computer and match them together in one?
I also want to change the framework behind this system to symfony2 so is there a way to solve this problem with this framework (e.g. doctrine?)
Thanks for your help!
Update:
My limitation is the Internet connection on the first computer which could not be eliminated.
If you really have limitations of (1) not being able to move the database off of the machine with a bad connection and (2) not being able to fix the bad connection; you are going to have to keep some sort of local instance on the second machine.
I would try to setup master-master replication from the first machine with the bad connection to the second machine. I'm not sure how reliable this will be considering the replication will be failing often due to the first machine's bad connection. This problem may be extrapolated if one or both machines are using old versions of MySQL. MySQL 5.5, for example, can be configured to actively monitor replication connectivity.
If the majority of your application does READS instead of WRITES, perhaps you could install Memcached (or something similar) on the second machine so that the application can pull data from local memory without requiring a connection to the MySQL server.
There are a few ways to achieve what you want (although maybe not exactly how you described), but the best way is definitely do host the database on a server that doesn't have Internet connectivity problems. Look for hosting that allows remote MySQL connections.

The right way? Automated Synchronization between 2 mysql DB

I already read a few threads here and I also went through the MySQL Replication Documentation (incl. Cluster Replication), but I think it's not what I really want and propably too complicated, too.
I got a local and a remote DB that might get both accessed/manipulated by 2 different persons at the same time. What I'd like to do is to sync them as soon as possible (means instantly or as soon the local machine goes online). Both DB's only get manipulated from my own PHP Scripts.
My approach is the following:
If local machine is online:
Let my PHP Script on the loal machine always send the SQL Query to the remote DB too
Let my PHP Script on the remote machine always store its queries and...
...let the local machine ask the remote DB every x minutes for new queries and apply them locally.
If local machine is offline:
Do step 2. also for both machines and send the stored queries to the remote DB as soon as
local machine goes online again. Also pull the queries from the remote machine for sure.
My questions are:
Did I just misunderstand Replication or am I right that my way would be easier in my case? Or is there any other good solution for what I'm trying to accomplish?
Any idea how I could check whether my local machine is online/offline? I guess I'd have to use JavaScript, but I don't know how. The browser/my script would always be running on the local machine.
What you're describing is master-master or multi-master replication. There are plenty of tutorials on how to set this up across the web. Definitely do your research before putting a solution like this into production as replication in MySQL isn't exactly elegant -- you need to know how to recover if (when?) something goes wrong.

MySQL: Join query across multiple databases located on different servers

In SQL Server there is a way to join tables from multiple sql servers by using link tables.
I wonder whether is it possible to do the same? I am using PHP, does PHP provides this kind of facilities?
Try the federated storage engine to link to tables on other servers.
http://dev.mysql.com/tech-resources/articles/mysql-federated-storage.html
It isn't necessarily easy, nor pretty, but this article gives some solutions to your problem:
http://www.linux.com/archive/feature/52390
UPDATE
Since the link is gone now, here is more text
Creating a linked server using OLE DB for SQL Server
This example creates a linked server named MyDatabase that uses the Microsoft OLE DB Provider for SQL Server.
USE master
GO
EXEC sp_addlinkedserver
'MyDatabase',
N'SQL Server'
GO
Then you can reference as though they are on the same server, so if the databases are on the same mssql server then skip the above step and just do the following:
[Server name].[database name].[owner].table_name

Categories