Not able to connect mysql - mysql

Not able to connect MySQL... detail given bellow.
A PHP Error was encountered
Severity: Warning
Message: mysqli::real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
Filename: mysqli/mysqli_driver.php
Line Number: 202
Backtrace:
File: /var/www/html/application/controllers/Account.php
Line: 7
Function: __construct
File: /var/www/html/index.php
Line: 315
Function: require_once

First you need to download xampp and ensure that my sql services on or off .if it is on then you are write function mysqli_connect(‘localhost’,’username’,’password’,’database’);

I guess you have a xampp installed in your system and SQL service was enabled.
If yes, then create one database file in your application/config folder. Filename is database.php
Here you need to give a default db driver variable, likewise
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'your-db-username',
'password' => 'your-db-password',
'database' => 'your-db-name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8mb4',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Next, you need to load this database.php config file in autoload.php (in the same directory).
$autoload['libraries'] = array('database');
Now, you have connected a database with your Codeigniter project.
Reference : https://www.codeigniter.com/userguide3/database/configuration.html

Related

Laravel 8 - How to solve Connection timed out while using remote MYSQL database?

In one of my Laravel 8 applications, I need to use two databases where one is a remote database. In my local development environment, the remote database is working fine (I can fetch data), but the problem arises only when I host the application in production. Every time it responds connection time out even for a simple query like SELECT * FROM users WHERE email = 'my_target_email_address'
Here is my .env file code:
# Local DB
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=bint_gt
DB_USERNAME=root
DB_PASSWORD=
# REMOTE DB
REMOTE_DB_CONNECTION=mysql
REMOTE_DB_HOST=REMOTE_DB_HOST
REMOTE_DB_PORT=3306
REMOTE_DB_DATABASE=REMOTE_DB
REMOTE_DB_USERNAME=REMOTE_DB_USER
REMOTE_DB_PASSWORD=REMOTE_DB_PASSWORD
Here is the updated code for the remote database in the config/database.php file:
'remote_mysql' => [
'driver' => 'mysql',
'url' => env('MFO_DATABASE_URL'),
'host' => env('MFO_DB_HOST'),
'port' => env('MFO_DB_PORT', '3306'),
'database' => env('MFO_DB_DATABASE', 'forge'),
'username' => env('MFO_DB_USERNAME', 'forge'),
'password' => env('MFO_DB_PASSWORD', ''),
'unix_socket' => env('MFO_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Finally, run the following MySQL query to fetch data:
$userEmail = Auth::user()->email;
$userInfo = DB::connection('remote_mysql')->select("SELECT * FROM users WHERE user_email='$userEmail'");
if( !empty($userInfo) ) {
$uid = $userInfo[0]->uid;
# do rest of the work
} else {
# return JSON response of user not found
}
Everything works fine in my local development environment (XAMPP), although the speed is a little bit slow, it does not work on the production server.
N.B: I am using Plesk rather cPanel.
Can anyone help me to figure out how to fix the problem?

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.

Codeigniter Cpanel Mysql Database Setup

I have successfully pushed a codeigniter project to a cpanel console.
Everything seems to be working fine except the database connection.
I believe i have the settings correct but i could be wrong.
Below is my database.php script:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'instadri_insta',
'password' => 'BL9w;K;ds9MR',
'database' => 'instadri_ver',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I have the errors below:
A PHP Error was encountered
Severity: Warning
Message: mysqli::real_connect(): (HY000/1044): Access denied for user
'instadri_insta'#'localhost' to database 'instadri_ver'
Filename: mysqli/mysqli_driver.php
Line Number: 202
Backtrace:
File: /home/instadri/application/controllers/Welcome.php Line: 10
Function: __construct
File: /home/instadri/public_html/index.php Line: 319 Function:
require_once
I also get :
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by
(output started at /home/instadri/system/core/Exceptions.php:272)
Filename: core/Common.php
Line Number: 568
Backtrace:
File: /home/instadri/application/controllers/Welcome.php Line: 10
Function: __construct
File: /home/instadri/public_html/index.php Line: 319 Function:
require_once
And Finally :
Unable to connect to your database server using the provided settings.
Filename: controllers/Welcome.php
Line Number: 10
I have taken care of the above.
The new issue now is the smarty template file loading.
I have a controller that does this :
$this->smarty->view('front-theme/index.tpl', $data );
But i get this error:
Unable to load the requested file:
front-theme/index.tpl
My cpanel directory structure is:
public_html
- index.php
application
system public
-_template
-_cache
-front-theme
I had the same problem but I have solved it.
Simply upload your CI files to the public html folder, every thing in the CI folder should be uploaded together. so when you open the public folder it should filled with the CI files.
Leave your db settings the same way, but cross-check the values you entered for validity. and finally go to the config.php file and check the base url variable and change to your url link name.
That's it!

Can i use mysql with yii framework?

I have xampp installed which includes mysql. Can i use this mysql with yii framework?
If yes how can i do it with phpmyadmin. Pls help. Am a newbie..
I have seen many people using sqlite with yii in many tutorials..
ok what you need to do is open /protected/config/main.php, and look for the line like this :
),
'db'=>array( // as you can see this db is currently in use
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
*/
just comment the first connection to sqlite data base and uncomment the second part that uses mysql database :
),
/*'db'=>array(
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
that's all!