如果主数据库关闭,如何连接到辅助数据库(PHP)
我希望有人能帮助我,我已经为此努力了3天。
i hope someone can help me, ive been struggling on this for 3 days now.
这是我的情况,我正在用php创建一个网站,并且我有2台计算机作为服务器与wampserver ...
here is my situation, i am making a website with php, and i have 2 computers as servers with wampserver...
主服务器为192.168.0.10
main server is 192.168.0.10
辅助服务器为192.168.0.12
secondary server is 192.168.0.12
我尝试远程连接是否正常的机器
and a virtual machine where im trying out if the remote conection works
我的网站托管在我的主服务器上,因此conexion查询是...
my website is hosted on my main server so the conexion query is...
$conexion = mysqli_connect("localhost","root","","dbdaq");
工作正常,我什至拥有复制服务器上的复制主机。
it works fine, i even have master to master replication on the servers.
但是我需要做的是,当主服务器关闭时,它需要尝试连接到seondary数据库上的数据库,所以我试图使用代码...
but what i need to do is that when main server is down it needs to try to conect to the database on the seondary database, so im trying to use the code...
$conexion = mysqli_connect("localhost","root","","dbdaq");
if (!$conexion){
$conexion = mysqli_connect("192.168.0.12","root","password","dbdaq");
}
但是当我手动打开主服务器上的mysql服务时,它实际上并没有尝试来使用其他服务器数据库...
but when i manually turn of the mysql services on main server it doesnt actually try to use the other servers database...
它一直给我说错误
警告:mysqli_connect ():(HY000 / 2002):由于目标计算机主动拒绝连接,因此无法建立连接。在C:\wamp64\www\PaginaV2\Pagina\PHP和JS\Procesoalumno.php在第2行上
Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it. in C:\wamp64\www\PaginaV2\Pagina\PHP y JS\Procesoalumno.php on line 2
尝试一下:使用php的 @
运算符(错误控制操作符),以抑制第一次连接失败时出现的错误消息。
Try this: use php's @
operator (the error control operator) to suppress the error message from the first connection failure. Try this.
$conexion = @mysqli_connect("localhost","root","","dbdaq");
if ( !$conexion ) {
$conexion = mysqli_connect("192.168.0.12","root","password","dbdaq");
}
故障转移连接的代码似乎正常工作,但仍然可以令人讨厌的错误消息。 @
运算符禁止显示该消息。
It seems likely your code for the failover connection works correctly, but you still get an obnoxious error message. The @
operator suppresses the message.