首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在服务器端的mysql db转储中自动替换用户密码(Laravel)

在服务器端的mysql db转储中自动替换用户密码(Laravel)
EN

Stack Overflow用户
提问于 2017-01-04 14:10:42
回答 7查看 1.7K关注 0票数 3

用例

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

问题

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

解决思路

在允许访问转储之前,在服务器端替换所有用户密码(所有用户用于开发目的的一个密码都可以)。

环境

  • Laravel应用
  • MySQL数据库
  • bash脚本可以在Capistrano上运行,也可以通过ssh在服务器上手动执行。

有什么办法聪明地解决这个问题吗?

示例转储(仅用户表)

代码语言:javascript
复制
# ************************************************************
# Sequel Pro SQL dump
# Version 4541
#
# http://www.sequelpro.com/
# https://github.com/sequelpro/sequelpro
#
# Host: 127.0.0.1 (MySQL 5.7.12)
# Datenbank: app
# Erstellt am: 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 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


# Export von Tabelle users
# ------------------------------------------------------------

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `role` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tenant_id` int(10) unsigned DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_tenant_id_role_unique` (`email`,`tenant_id`,`role`),
  KEY `users_tenant_id_index` (`tenant_id`),
  CONSTRAINT `users_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;

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

/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;

/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2017-01-13 10:23:52

我建议这样做:

代码语言:javascript
复制
create table users_tmp like users;
insert into users_tmp select * from users;
update users_tmp set password='whatever';

然后进口后

代码语言:javascript
复制
rename table users_tmp to users;
票数 2
EN

Stack Overflow用户

发布于 2017-01-12 16:17:32

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

代码语言:javascript
复制
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.');
            }
        }
    }

}
票数 1
EN

Stack Overflow用户

发布于 2017-01-15 00:02:08

用例:

  • 您可以对生产数据库进行转储,将DB导入测试/阶段env并更新用户表。用于用户匿名的SQL: 更新用户集mobile_phone = NULL,private_phone = NULL,email = IF(isnull(email),NULL,concat(' User ',id,'@test.local')),密码= MD5('code');
  • 开发人员只应该从测试/阶段env中转储DB。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41465842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档