From f4604e126570ca82da17b3d614f0385f5023d7f9 Mon Sep 17 00:00:00 2001 From: kira <529280602@qq.com> Date: Sat, 23 Jun 2018 23:42:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8Cinstall?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E4=B8=BB=E6=9C=BA=E4=B8=8D=E6=98=AF127.0.0.1?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=AE=89=E8=A3=85=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 执行php think install --hostname 172.17.0.1 --password 1234 --force true的时候,假如用的主机名不是127.0.0.1的话,会安装失败,因为DB实例连接到本地了 --- application/admin/command/Install.php | 42 ++++++++++++++++----------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/application/admin/command/Install.php b/application/admin/command/Install.php index 149aefd10..db6ccd0e9 100644 --- a/application/admin/command/Install.php +++ b/application/admin/command/Install.php @@ -20,15 +20,15 @@ class Install extends Command { $config = Config::get('database'); $this - ->setName('install') - ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname']) - ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport']) - ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database']) - ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix']) - ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username']) - ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password']) - ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE) - ->setDescription('New installation of FastAdmin'); + ->setName('install') + ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname']) + ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport']) + ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database']) + ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix']) + ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username']) + ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password']) + ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE) + ->setDescription('New installation of FastAdmin'); } protected function execute(Input $input, Output $output) @@ -43,8 +43,7 @@ class Install extends Command $password = $input->getOption('password'); $installLockFile = __DIR__ . "/Install/install.lock"; - if (is_file($installLockFile) && !$force) - { + if (is_file($installLockFile) && !$force) { throw new Exception("\nFastAdmin already installed!\nIf you need to reinstall again, use the parameter --force=true "); } @@ -58,21 +57,30 @@ class Install extends Command $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->query("CREATE DATABASE IF NOT EXISTS `{$database}` CHARACTER SET utf8 COLLATE utf8_general_ci;"); + // 连接install命令中指定的数据库 + $instance = Db::connect([ + 'type' => 'mysql', + 'hostname' => "{$hostname}", + 'hostport' => "{$hostport}", + 'database' => "{$database}", + 'username' => "{$username}", + 'password' => "{$password}", + ]); + // 查询一次SQL,判断连接是否正常 - Db::execute("SELECT 1"); + $instance->execute("SELECT 1"); // 调用原生PDO对象进行批量查询 - Db::getPdo()->exec($sql); + $instance->getPdo()->exec($sql); file_put_contents($installLockFile, 1); $dbConfigFile = APP_PATH . 'database.php'; $config = @file_get_contents($dbConfigFile); - $callback = function($matches) use($hostname, $hostport, $username, $password, $database, $prefix) { + $callback = function ($matches) use ($hostname, $hostport, $username, $password, $database, $prefix) { $field = $matches[1]; $replace = $$field; - if ($matches[1] == 'hostport' && $hostport == 3306) - { + if ($matches[1] == 'hostport' && $hostport == 3306) { $replace = ''; } return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),"; @@ -80,7 +88,7 @@ class Install extends Command $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config); // 写入数据库配置 file_put_contents($dbConfigFile, $config); - + \think\Cache::rm('__menu__'); $output->info("Install Successed!"); -- Gitee From 03f6ba62d6d163346b3d3164576139d23bf7118a Mon Sep 17 00:00:00 2001 From: kira <529280602@qq.com> Date: Sat, 23 Jun 2018 23:51:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8Cinstall?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E4=B8=BB=E6=9C=BA=E4=B8=8D=E6=98=AF127.0.0.1?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=AE=89=E8=A3=85=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 执行php think install --hostname 172.17.0.1 --password 1234 --force true的时候,假如用的主机名不是127.0.0.1的话,会安装失败,因为DB实例连接到本地了 --- application/admin/command/Install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/admin/command/Install.php b/application/admin/command/Install.php index db6ccd0e9..5c655531d 100644 --- a/application/admin/command/Install.php +++ b/application/admin/command/Install.php @@ -59,7 +59,7 @@ class Install extends Command // 连接install命令中指定的数据库 $instance = Db::connect([ - 'type' => 'mysql', + 'type' => "{$config['type']}", 'hostname' => "{$hostname}", 'hostport' => "{$hostport}", 'database' => "{$database}", -- Gitee