Master Slave Configuration in Laravel 5.5 - mysql

How to configure Laravel 5.5 with master slave MySQL replication ?
I want to make write operations and read operations in master and slave respectively .
Optional: Is there any way to do connection pooling and max / min no of open connections in ideal conditions. ?

Just change your config/database.php file to include read (slave) and write (master) hosts like so like the Laravel docs suggest:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],

Related

TYPO3: Access denied to database after moving installation

I have migrated successfully my TYPO3 v10 site under mampp. Now I get this exception:
Doctrine\DBAL\Exception\ConnectionException An exception occurred in driver: Access denied for user 'dbxx'#'localhost'
My configuration entry is the following:
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8mb4',
'dbname' => 'mysite_f10',
'driver' => 'mysqli',
'host' => 'localhost',
'password' => 'root',
'tableoptions' => [
'charset' => 'utf8mb4',
'collate' => 'utf8mb4_unicode_ci',
],
'user' => 'root',
],
],
],
Is it possible, that TYPO3 stores still my old database values in a cache? And if, how can I clear my cache manually?
Database credentials are not cached anywhere. I assume that you changed the values in LocalConfiguration.php. You should check your installation if there is a file called AdditionalConfiguration.php in the same directory as LocalConfiguration.php. All values in that file overwrite values from LocalConfiguration.php.

Laravel: Database SSL connection not working

I tested the ssl connection with my (Open SSL) generated certificates via the MySQL workbench app and it was working perfectly fine via ssl. However, when I try to use a ssl connection to my remote database with Laravel I get errors. On my local machine I get a 502 Bad Gateway error when visiting my laravel website and on my online test setup I get a SQLSTATE[HY000] [2002] error.
Config:
'mysql' => [
'driver' => 'mysql',
'host' => env( 'DB_HOST', '127.0.0.1' ),
'port' => env( 'DB_PORT', '3306' ),
'database' => env( 'DB_DATABASE', 'forge' ),
'username' => env( 'DB_USERNAME', 'forge' ),
'password' => env( 'DB_PASSWORD', '' ),
'unix_socket' => env( 'DB_SOCKET', '' ),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'modes' => [
//'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/client-key.pem'),
PDO::MYSQL_ATTR_SSL_CERT => base_path('ssl/client-cert.pem'),
PDO::MYSQL_ATTR_SSL_CA => base_path('ssl/ca-cert.pem')
]
],
This is an error that is logged in my online setup in the laravel log file:
[previous exception] [object] (PDOException(code: 0): PDO::__construct(): Unable to locate peer certificate CN at /var/www/vhosts/website.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:38)
[stacktrace]
I'd really appreciate any hints.
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,

Trying to connect laravel to mysql but i keep getting an error

I have my laravel app running and i'm trying to connect it to a Mysql database. I'm using XAAMP for this. After getting Apache and Mysql is running (Mysql is running on port 3308). I reconfigured my .env file and database.php file and I've run php artisan config cache. However when i try to create a database from my terminal using create database xxx, xxx database is created but when i check phpmyadmin on my browser, i can't find the xxx datatbase.
Here's my database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'test123'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
And here's my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3308
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=test123
Solved it but i had to clear up port 3306 so that XAAMP mysql could use it. Cleared it by ending all mysql tasks under task manager.

Yii2 Read Write splitting couldn't connect slave server in master slave configuration

Have done Master Slave configuration as per official Yii2 documentation. Below is actual configuration look like,
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=1.1.1.1;dbname=master_db',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'enableSchemaCache' => true,
'schemaCacheDuration' => 10,
'schemaCache' => 'cache',
'slaveConfig' => [
'username' => 'slave_user',
'password' => 'slave_password',
'charset' => 'utf8',
'attributes' => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],
'enableSchemaCache' => true,
'schemaCacheDuration' => 10,
'schemaCache' => 'cache',
],
'slaves' => [
['dsn' => 'mysql:host=2.2.2.2;dbname=slave_db']
],
],
It always connect master database even if slave server is up and reachable.
Surprisingly replacing current master config with slave one works, moreover if try to connect slave database from command line it get connected in a moment but unable to achieve same with above configuration.
Wondering if there is any parameters missing in configuration or any other way to get things working like ideal read write splitting?
issue was resolved by adding connection class in the slaveConfig,
'class' => 'yii\db\Connection'

Does yii2 have persistent connection options?

I can't find documentation about Yii2 persistent connection.
I have problems with Yii2 behavior. It always opens the connection and closes it after executing the query. I think creating a persistent connection is the answer to my problems.
How to do that?
See the following github issue:
'db' => array(
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'tablePrefix' => '',
'charset' => 'utf8',
'attributes'=>[
PDO::ATTR_PERSISTENT => true
]
),