一、配置config/database.php
新增mysql连接
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => '2346168_online',
'username' => 'n2346168_online',
'password' => '7pOIzzIw09(-*ibXNo5hHVb',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
二、同步方法
public function databaseSync(Request $request)
{
//接收要更新的表参数
$table = $request->input('table');
$table_arr = [
'user_member' => [
'new_table' => 'image_user_member',
'column' => '(`id`, `phone`, `qq`, `wechat`, `nickname`, `avatar`, `token`, `expiry_time`, `regtime`, `status`, `pur_amount`, `pur_times`, `last_pur`, `vip_end_time`, `free_times`, `appid`, `vip_times`, `ext_config`, `qudao`, `order_situation`, `id_photo_times`, `guineas`, `listen_times`, `compositing_times`, `guineas_situation`, `cash_pay`)',
'where' => ['appid' => 4]
],
'order_table' => [
'new_table' => 'image_order_table',
'column' => '(`id`, `order_id`, `group_id`, `user_id`, `amount`, `create_time`, `pay_time`, `pay_status`, `pay_method`, `days`, `vip_times`, `appid`, `qudao`, `from_fun`, `mch_id`, `order_situation`, `renewal_time`, `into_account`, `type`, `payment_type`, `guineas`, `order_specs`)',
'where' => ['appid' => 4]
],
'valid_order' => [
'new_table' => 'image_valid_order',
'column' => '(id, user_id, group_id, order_id,start_time,end_time)',
'where' => []
],
'user_member_op_log' => [
'new_table' => 'image_user_member_op_log',
'column' => '(id, op_type, user_id, op_time,op_id)',
'where' => []
],
'order_op_log' => [
'new_table' => 'image_order_op_log',
'column' => '(`id`, `op_type`, `order_id`, `op_time`, `op_id`, `appid`, `user_id`)',
'where' => ['appid' => 4]
],
'product_statistics' => [
'new_table' => 'image_product_statistics',
'column' => '(`id`, `user_id`, `appid`, `image_to_word_time`, `photo_translation_time`, `scanning_time`, `recording_time`, `video_to_audio_time`, `audio_clips`, `date_time`, `image_to_text_translation`, `retranslate`, `id_photo`, `image_to_form`, `form_identify`, `audio_segmentation`, `audio_merge`, `audio_format_conversion`, `real_time_transfer`,`voice_to_text`,`handwriting_recognition`,`automatic_segmentation`)',
'where' => ['appid' => 4]
],
'mobile_login_log' => [
'new_table' => 'mobile_login_log',
'column' => '(`id`, `user_id`, `status`, `result`, `create_time`, `appid`)',
'where' => ['appid' => 4]
],
'start_log' => [
'new_table' => 'start_log',
'column' => '(`id`, `user_id`, `appid`, `qudao`, `date`)',
'where' => ['appid' => 4]
],
];
//同步用户表
$list = DB::connection('mysql2')->table($table)->where($table_arr[$table]['where'])->get(); //旧站数据
$count = $list->count();//总数
$list = $list->toArray();
$page = ceil($count / 1000);
for ($i = 1; $i <= $page; $i++) {
$str = '';
for ($l = 0; $l < 1000; $l++) {
$values = '';
$num = ($i - 1) * 1000 + $l;
if (isset($list[$num])) {
if ($l > 0) {
$str .= ",";
}
$values .= '(';
foreach ($list[$num] as $k => &$item) {
if ($k != 'id') {
$values .= ',';
}
if (is_null($item)) {
$item = 'null';
$values .= $item;
} else {
if (is_string($item)) {
$values .= "'" . addslashes($item) . "'";
} else {
$values .= "'{$item}'";
}
}
}
$values .= ')';
$str .= $values;
} else {
break;
}
}
DB::connection('mysql')->insert("insert ignore into `{$table_arr[$table]['new_table']}` {$table_arr[$table]['column']} values {$str}");
}
echo "<p>$table sync finished</p>";
}