MySQL error unknown collation - mysql

After attempting to run a migration in Laravel 4 I received the following error:
[PDOException]
SQLSTATE[HY000]: General error: 1273 Unknown collation: ''
Here is my database connection configuration;
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
),
Any suggestions?

Grab the latest source code by running composer update from the root folder of your installation. Else update your array as below.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

Related

Copy values from one database to another using laravel

I'm using two different databases, one called "example" and another "example_hist". I'm trying to create a cron job that verifies the database "example" in table "users" if there is any user that has a state "disabled".
If there is, it should copy the user row to the table "users" inside the database "example_hist"
My problem is that I can't figure out how I can do this.
I already did the connection:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'unix_socket' => env('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'),
]) : [],
],
in my .env I have:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=example
DB_USERNAME=root
DB_PASSWORD=pass
DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=example_hist
DB_USERNAME_2=root
DB_PASSWORD_2=pass
I already inserted the command in the kernel and this is the code I have now:
public function handle()
{
$users = DB::table('users')->where('state', 'Disabled')->get();
$user_hist = DB::connection('mysql2')->select('users');
//should be something here that copies the row from $users
}
Create one more field as copied which will be default false, and it will be true once its copied to other database
Run query whose state is disabled and not copied
$users = DB::table('users')->where('state', 'Disabled')
->where("copied", "=", 0)->get();
Run users loop
foreach ($users as $user){
//Now using connection insert into another table of another db
DB::connection('mysql2')->insert('insert into users (name, email, mobile, password, created_at, state) values (?,?,?,?,?,?)', array($user->name , $user->email , $user->mobile, $user->password, $user->created_at, $user->state ) );
//Make user copied status to true so next time it wont copied again
$user->copied = true;
$user->save();
}
Well, with the help of #Teekay I could manage to find myself the right solution. This worked for me. Hope this can help anyone with the same problem.
public function handle()
{
$users = DB::table('users')->where('state', 'Disabled')->where("copied",'false')->get();
if($users){
foreach ($users as $user){
DB::connection('mysql2')->insert('insert into users (name, email, mobile, password, created_at, state) values (?,?,?,?,?,?)', array($user->name , $user->email , $user->mobile, $user->password, $user->created_at, $user->state ) );
DB::table('users')->where('state', 'Disabled')->where("copied", 'false')->update(['copied' => 'true']);
}
}
}

How to change db name for yii2 Query Builder

iam using multiple databases for my yii2 project.In my config file, I have the configuration like
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
],
And i have a query like
$query = (new \yii\db\Query())
->select(['A.id', 'A.category_english', 'COUNT(B.category_id)'])
->from(['A' => 'tbl_categories', 'B' => $filteredData])
->where('A.id = B.category_id')
->groupBy(['A.id', 'A.category_english'])
->orderBy(['COUNT(B.category_id)'=>SORT_DESC]);
$data = $query->all();
The above query is fetching details from the first DB. How can I change that to the second? Where I can set the db name in query builder.

How to connect mysql database from the diffarent server to localhost xampp in codeigniter

I have to connect database from my domain example (www.test.com) to other project on localhost xampp thought codeigniter platform.
I have tried below code in aplication/config/database.php
Note: I have not mentioned exact host name, database, username & password for security reasons.
$db['default'] = array(
'dsn' => '',
'hostname' => 'XXX.XX.XX.XXX:3306',
'username' => '****_cs_user',
'password' => '********',
'database' => '****_case_study',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
You need to ensure that this options are correctly filled:
$db['default'] = array(
'dsn' => '',
'hostname' => 'yourhostip',
'username' => 'yourmysluser',
'password' => 'yourmysqlpassword',
'database' => 'yourdb',
'dbdriver' => 'mysqli',
'port' => '3306'
// Rest of options
);
According to the Codeigniter documentation, you can separate the port from the hostname.
You should make sure that in your server is activated the remote access, by default is disabled. Check this post to enable this feature.

Zend-F2: How to configure "Mysqli: The ext/mysqli driver"

I am using ZF v2.2. I am having a problem during database connectivity with "Mysqli: The ext/mysqli driver". Actually i am unable to figure out the process of "connecting to database & then using $sql->select(), $sql->update() etc".
Any idea how to configure this?
File: autoload/db.local.php
return array(
'db' => array(
'driver' => 'Mysqli',
'username' => 'root',
'password' => 'redhat',
'database' => 'mydb',
'host' => 'localhost'
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
);
public function indexAction() {
$db=$this->getServiceLocator()->get('db'); ` `echo $db->getDriver()->getConnection()->isConnected();
//**returns false**.
}

Laravel 4 artisan migrate install

I'm trying to migrate a very basic app from Laravel 3 to Laravel 4. It looks very easy but for some reason the following command is not working.
php artisan migrate:install
Following this tutorial: http://laravel.com/docs/quick#installation it seems like we don't need to install the artisan anymore. So I tried just running
php artisan migrate
But I always get the error
[ErrorException]
Undefined index: collation
My database.php:
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'wepromoters_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
My migration:
public function up()
{
Schema::create('users',function($table){
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->string('type');
$table->timestamps();
});
}
Lastly the db is working fine with my Laravel 3 app
What is going on?
According to this thread on the Laravel forums, a collation parameter is now required in the database configuration.
Try it with this (notice the extra "collation" parameter after the "charset" one) :
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'wepromoters_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Laravel 4 is entirely different with Laravel 3, So use laravel 4 commands to migrate your table. I don't know exactly but you can try with this command:
php artisan make:migration create_users_table(Enter)
So, we created default users table and have to create our table:
php artisan make:migration create_YourTableName_table -- create YourTableName
Then, check your file in "migration/YourTableName.php" by opening it.
Now, you can write your code under the "Run Migration Block".
Finally, Run this command to migrate your table.
php artisan migrate
It will migrate your table with in span of time.