Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动替换服务器端mysql数据库转储(Laravel)中的用户密码_Php_Mysql_Bash_Laravel_Laravel 5 - Fatal编程技术网

Php 自动替换服务器端mysql数据库转储(Laravel)中的用户密码

Php 自动替换服务器端mysql数据库转储(Laravel)中的用户密码,php,mysql,bash,laravel,laravel-5,Php,Mysql,Bash,Laravel,Laravel 5,用例 要设置开发环境,需要应用程序数据库的mysql转储。转储源是生产数据库 问题 我不想给任何人转储,因为它包含(散列)用户密码 解决方案理念 在授予对转储的访问权限之前,替换服务器端的所有用户密码(一个用于开发目的的密码就可以了) 环境 拉维应用 MySQL数据库 bash脚本可以在Capistrano上运行,也可以通过ssh手动在服务器上执行 有什么办法可以聪明地解决这个问题吗 示例转储(仅限用户表) #*****************************************

用例

要设置开发环境,需要应用程序数据库的mysql转储。转储源是生产数据库

问题

我不想给任何人转储,因为它包含(散列)用户密码

解决方案理念

在授予对转储的访问权限之前,替换服务器端的所有用户密码(一个用于开发目的的密码就可以了)

环境

  • 拉维应用
  • MySQL数据库
  • bash脚本可以在Capistrano上运行,也可以通过ssh手动在服务器上执行
有什么办法可以聪明地解决这个问题吗

示例转储(仅限用户表)

#************************************************************
#Sequel Pro SQL转储
#版本4541
#
# http://www.sequelpro.com/
# https://github.com/sequelpro/sequelpro
#
#主机:127.0.0.1(MySQL 5.7.12)
#Datenbank:应用程序
#厄斯特尔特上午:2017-01-04 14:22:35+0000
# ************************************************************
/*!40101 SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101 SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101设置@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101集合名称utf8*/;
/*!40014设置@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;
/*!40101设置@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;
/*!40111 SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/;
#导出von Tabelle用户
# ------------------------------------------------------------
如果存在“用户”,则删除表;
创建表“用户”(
`id`int(10)无符号非空自动增量,
`角色'varchar(255)COLLATE utf8\u unicode\u ci NOT NULL,
`租户_id`int(10)无符号默认为空,
`name`varchar(255)COLLATE utf8\u unicode\u ci NOT NULL,
`电子邮件'varchar(255)校对utf8\u unicode\u ci非空,
`密码'varchar(60)COLLATE utf8\u unicode\u ci NOT NULL,
`记住\u token`varchar(100)COLLATE utf8\u unicode\u ci DEFAULT NULL,
`在“timestamp NULL DEFAULT NULL”处创建了_,
`在`时间戳为空默认为空'处更新了_,
主键(`id`),
唯一密钥'users\u email\u tenant\u id\u role\u UNIQUE'('email','tenant\u id','role'),
键“用户\租户\ id \索引”(“租户\ id”),
约束`users\u tenant\u id\u foreign`外键(`tenant\u id`)在删除级联上引用`tenants`(`id`)
)ENGINE=InnoDB默认字符集=utf8 COLLATE=utf8\U unicode\U ci;
锁表`用户'写入;
/*!40000 ALTER TABLE`users`禁用键*/;
在“用户”中插入(`id`、`role`、`tenant\u id`、`name`、`email`、`password`、`remember\u token`、`created\u at`、`Update\u at`)
价值观
(1,'admin',1,'appadmin','admin@example.com“,”123$15$g4qKHcS7zHercuNJobfFxOTWGoW7YN.tphFdddGIIEXkrkE8Etxxx',空,'2017-01-04 14:19:04','2017-01-04 14:19:04');
/*!40000 ALTER TABLE`users`启用键*/;
解锁表格;
/*!40111设置SQL_注释=@OLD_SQL_注释*/;
/*!40101设置SQL_模式=@OLD_SQL_模式*/;
/*!40014设置外键检查=@OLD_FOREIGN_KEY_CHECKS*/;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101集字符集结果=@旧字符集结果*/;
/*!40101设置排序规则\连接=@OLD\ u排序规则\连接*/;
  • 使用mysqldump选项
    mysqldump-d应用程序
    -无数据转储所有数据
    mysqldump-t--ignore table=app.users app
    -仅转储数据,但排除用户表

  • 在转储文件中搜索密码模式并替换它,例如,使用linux sed:
    sed-i's/''''''''\$2y\$[0-9]\{2\}\$.\{53\''''''''/''''''''''new-hash-here'\'''/g'dump.sql


  • 我对控制台环境正则表达式不太熟练。但是转储数据,然后做类似的事情不是更好吗

    (^\s*\([0-9]+,'.*',[0-9]+,'.*','.*',)'.*'(,.*,.*,.*\))
    
    并替换为

    \1'NEW_HASH'\2
    

    这里有一个解决方案,使用的是命令行脚本包装器,这是一个用于混淆SQL转储的库

    首先,安装
    strike
    实用程序(需要在系统上安装
    ruby
    ):

    接下来,运行
    strike obfuscate
    以模糊转储。以下示例将用固定字符串
    密码“
    ”替换
    用户
    表中的所有
    密码
    值:

    strike obfuscate
    命令的输出将返回整个SQL转储内容,但插入行被修改为使
    密码
    值模糊:

    INSERT INTO `users` (`id`, `role`, `tenant_id`, `name`, `email`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES ('1','admin','1','appadmin','admin@example.com','password',NULL,'2017-01-04 14:19:04','2017-01-04 14:19:04');
    

    创建一个简单的命令替换用户表中的所有(非管理员)电子邮件地址和密码,然后转储数据库。下面受密码保护的命令将更改不使用给定域的所有电子邮件地址,然后更改所有帐户的密码。在
    fire
    方法的末尾,添加artisan命令以转储数据库。(假设你已经有了一个包)


    我建议使用sql方式执行此操作:

    create table users_tmp like users;
    insert into users_tmp select * from users;
    update users_tmp set password='whatever';
    
    那么进口之后呢,

    rename table users_tmp to users;
    
    用例:

    • 您可以转储生产数据库,将数据库导入test/stage env并更新用户表。 用于用户匿名化的SQL:

      UPDATE User
      SET
        mobile_phone  = NULL,
        private_phone = NULL,
        email = IF(isnull(email), NULL, concat('user', id, '@test.local')),
        password = MD5('code')
      ;
      
    • 开发人员应该只从测试/阶段环境中转储DB


      • 要以一种聪明的方式解决这个问题,那么,不要转储数据库

        为了获得更好的可行开发环境,您最好:

        • 使用模式转储(例如:with
          mysqldump--no data
          模式数据库迁移(我相信laravel有内置的)模式规范(如果用于例如:spreep)来创建表
        • 使用fixture和/或console命令向数据库填充任何非特殊开发任务所需的内容(付款或其他奇怪的东西)
        • 使用集装箱化工具,如docker和/或vagrant(我两者都使用)class RelacePasswordsAndEmailsCommand extends Command { protected $name = 'mycommand/safe_user_dump'; protected $description = 'Changes all email addresses and passwords for a safe dump'; public function __construct() { parent::__construct(); } public function fire() { if ($this->confirm('WARNING! This will change all email addresses and passwords in the database. Do you wish to continue? [yes|no]')) { $password = $this->secret('What is the password?'); if ( $password === 'mysecretpassword' ) { $emails = DB::table('users') ->where(function ($q) { return $q->where('email', 'NOT LIKE', '%@example.com'); })->lists('id'); foreach ($emails as $id ) { DB::table('users')->where('id', $id)->update(['email' => 'user' . $id . '@example.com']); } DB::table('users')->update(['password' => 'myhashpassword']) $this->info( count($emails) . ' email addresses and passwords changed.'); # Add your database dump command here } else { $this->info('The password was incorrect.'); } } } }
          create table users_tmp like users;
          insert into users_tmp select * from users;
          update users_tmp set password='whatever';
          
          rename table users_tmp to users;
          
          UPDATE User
          SET
            mobile_phone  = NULL,
            private_phone = NULL,
            email = IF(isnull(email), NULL, concat('user', id, '@test.local')),
            password = MD5('code')
          ;