Async Mysql Client of Swoole in my PHP Project for which I am trying to use Swoole MySQL Class of PHP but I am getting
class not found
error while executing the code.
Below is the code snippet I was trying to execute:
<?php
use Swoole\MySQL as MySQL;
MySQL::connect(array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'Secret_Password',
'database' => 'swoole_test_db',
'charset' => 'utf8',
'timeout' => 2,
), function ($mysql_client) {
$mysql_client->query("SELECT `reg_id`, `name` FROM `students` WHERE `reg_id` = 8;", function ($result) {
var_dump($result);
});
});
I tried searching everywhere but unable to find any reference on this error.
Version Details
PHP 7.3.2
Swoole Version 4.4.3 (with Swoole MySQL selected Yes during installation)
Did I miss something, please help?
Thanks in advance!!!
It seems that the official documentation is either a bit off, or mentions a previous version of Swoole.
The class that should be used is Swoole\Coroutine\MySQL.
You can verify that using the official documentation in their GitHub repository.
Related
I have a laravel project with many connections to different IP's.
I want laravel to connect to a backup database in the case that the main SQL server was down
Example.
192.168.1.2 -> SQL DB #1
192.168.1.3 -> SQL DB #1 Backup
If 192.168.1.2 goes down, laravel must connect to 192.168.1.3
I'd like to do this in database.php file, but I think that's impossible.
I was trying to test connection before make a query like this:
if(DB::connection('connection')->getDatabaseName())
but it seems that it save data in cache and it still throw database name even if I shutdown the SQL server
For this answer, I'm considering Laravel 5.
By debugging a model query, I've found out that Laravel connections support not a single host, but a list of them.
[
'driver' => 'sqlsrv',
'host' => [
'192.168.1.2',
'192.168.1.3',
],
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'transaction_isolation' => PDO::SQLSRV_TXN_READ_UNCOMMITTED, // Not required, but worth mentioning it's possible to define it here too
'options' => [],
]
The underlying method behind Laravel connections resolving is Illuminate\Database\Connectors::createPdoResolverWithHosts which has the following behavior:
protected function createPdoResolverWithHosts(array $config)
{
return function () use ($config) {
foreach (Arr::shuffle($hosts = $this->parseHosts($config)) as $key => $host) {
$config['host'] = $host;
try {
return $this->createConnector($config)->connect($config);
} catch (PDOException $e) {
continue;
}
}
throw $e;
};
}
Such behavior means that Laravel will randomly pick one of the connection's hosts and try to connect to them. If the attempt fails, it keeps trying until no more hosts are found.
You could define two mysql connections in app/config/database.php
and using a middleware you could define the db that should be connected to.
You can find a more elaborate explanation in this URL:
http://fideloper.com/laravel-multiple-database-connections
i recently started searching for the same thing and to change the connection as soon as possible you can either
add the check inside a service provider
or through a global middleware
try{
\DB::connection()->getPdo(); // check if we have a connection
}catch{
\DB::purge(config('database.default')); // disconnect from the current
\DB::setDefaultConnection('my-fallback-db'); // connect to a new one
}
also check laravel api docs for more info.
I want use Yii and MS Access together, but I don't know how..
I can use ODBC without Yii like this
$link = odbc_connect($name, $user, $pass)
but now i need it in Yii, like MySQL:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=ex1c',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'ex1c_',
)
what should i write in connection string
may be need something software setup in computer
how to set it?
If I'm understanding your question right, you have one set of data that you want to be able to reference from a YII application and from MS Access. The best approach I can think of here is to:
Put the data into a MySQL database
Have YII access it directly as you illustrate above
Set up the MySQL ODBC driver on the Windows machine where you are running access, and point Access at MySQL via that driver
Here is a pointer to MyODBC, which will provide the critical linkage between Access and MySQL:
http://dev.mysql.com/downloads/connector/odbc/
It seems that ZF2 with the PDO:dblib driver defaults to using the SQL92 SQL decorator. How do I use the DI system to tell it to use the SqlServer decorator instead? Just can't get my head around it.
The issue is that the paginator attempts to use LIMIT/OFFSET syntax which doesn't work in SQL Server.
Another way of phrasing it is how do I customise the Paginator returned from fetchAll() on a table object to use the SqlServer platform?
Chris
OK, well I found there's a much easier way to do it, after much digging through the Zend Framework. Simply set up your database config like this:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'dblib:host=xxx;dbname=xxx',
'username' => 'xxxxxxxx',
'password' => 'xxxxxxxx',
'charset' => 'UTF-8',
'platform' => 'SqlServer'
)
Now I'm getting different errors, will continue the story!
How to install and use SmallFry framework.
I downloaded it from https://github.com/maniator/SmallFry
Extracted it under htdocs and placed all content in folder: smallfry (C:\xampp\htdocs\smallfry).
Set Doc root to define('DOCROOT', 'C:/xampp/htdocs'); in Autoloader.php.
Changed db info to following settings---
$CONFIG->set('DB_INFO', array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'smallfry',
));
Still getting the following error --
let me know what else need to be change as i m very new to this framework.
Apparently, the framework is a bit out-dated, as assigning the return vlaue of new by reference is indeed deprecated. You should look for $x =& new Obj; calls and remove the &.
The Fatal error should be fixed after fixing the other issues. According to the documentation, that method should exist.
Fatal error occurs due to first two errors.
Because of Deprecated warnings Config class wasn't loaded correctly.
I'm hosting a Yii app on shared-host with some my friend, and keep database in private MySQL server. As you knew, database info can be found so very easy in protected\config\main.php by another host owner (my friend and more):
'db'=>array(
'connectionString' => 'mysql:host=211.113.2.45;dbname=FamilyBook',
'emulatePrepare' => true,
'username' => root,
'password' => 'xcute445',
'charset' => 'utf8',
),
Is there any solution to conceal connection information as IP mySQL server, username, password?
May MySQL server provide RSA mechanism to protect database info?
Example, any people can see as below but cannot understand or use:
'db'=>array(
'connectionString' => '57bf064b2166366a5ea61109006b8d5c',
'emulatePrepare' => true,
'username' => '63a9f0ea7bb98050796b649e85481845',
'password' => 'e04ccf211208f8c97e4a36e584926e60',
'charset' => 'utf8',
), // value by MD5 function, example only
No, you cannot conceal the credentials from someone who has access to your source as long as you are using native MySql authentication. That's because your code needs to pass the credentials as cleartext¹ to the server, so it needs to be able to "decrypt" them before connecting. Someone who has access to your source can follow the same procedure and decrypt them as well.
You could secure your system by relying on some type of PAM authentication instead of user-supplied credentials, but Yii does not support such.
¹note: This is not actually true. The client passes a hash to the server, but it needs to have access to the original password in order to hash it. This means that for the purposes of this discussion it makes no difference (it would make a difference for someone who is listening on the network).
Using Yii 1.x I did it using below method.
create a class, DbConnection inside protected/components extending from CDbConnection
class DbConnection extends CDbConnection
{
public function createPdoInstance()
{
// Decrypt the password used in config file
// e.g.: $this->password = mydecrypt($this->password);
return parent::createPdoInstance();
}
}
Adjust the config file (protected/config/main.php)
'db' => array(
'class' => 'DbConnection', // Use above classname
'password' => 'encryptedpassword',
),