{"id":13569089,"url":"https://github.com/globalis-ms/mysql-data-anonymizer","last_synced_at":"2025-07-27T10:33:36.058Z","repository":{"id":75640479,"uuid":"185165983","full_name":"globalis-ms/mysql-data-anonymizer","owner":"globalis-ms","description":null,"archived":false,"fork":false,"pushed_at":"2019-12-02T09:33:09.000Z","size":61,"stargazers_count":34,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-25T02:23:32.205Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/globalis-ms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-05-06T09:30:15.000Z","updated_at":"2025-02-25T04:34:25.000Z","dependencies_parsed_at":"2023-06-07T05:15:06.515Z","dependency_job_id":null,"html_url":"https://github.com/globalis-ms/mysql-data-anonymizer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/globalis-ms/mysql-data-anonymizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/globalis-ms%2Fmysql-data-anonymizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/globalis-ms%2Fmysql-data-anonymizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/globalis-ms%2Fmysql-data-anonymizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/globalis-ms%2Fmysql-data-anonymizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/globalis-ms","download_url":"https://codeload.github.com/globalis-ms/mysql-data-anonymizer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/globalis-ms%2Fmysql-data-anonymizer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267344945,"owners_count":24072425,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T14:00:35.885Z","updated_at":"2025-07-27T10:33:36.012Z","avatar_url":"https://github.com/globalis-ms.png","language":"PHP","funding_links":[],"categories":["Awesome Privacy Engineering [![Awesome](https://awesome.re/badge.svg)](https://awesome.re)"],"sub_categories":["De-Identification and Anonymization"],"readme":"# MySQL Data Anonymizer\n\nMySQL Data Anonymizer is a PHP library that anonymize your data in the database.\nAlways use the production database to test your programs, but worry about leaking cutomer data?\nMySQL Data Anonymizer is the right tool for you. This tool helps you replace all sensitive data with fake data.\nFake data is provided by a [fzaninotto/Faker](https://github.com/fzaninotto/Faker) generator by default, but you can also use your own generator.\nTo improve the performance, [AMP/MySQL](https://github.com/amphp/mysql) is used to create multiple MySQL connections concurrently.\n\nMySQL Data Anonymizer requires PHP \u003e= 7.2.\n\n# Table of Contents\n\n- [Configuration](#configuration)\n- [Example code](#example-code)\n- [Helpers and providers](#helpers-and-providers)\n\n\n\n## Configuration\n\nRename the config-sample.php file to config.php and modify the configurations to suit your environment.\n```php\n\u003c?php return array (\n    'DB_HOST' =\u003e '127.0.0.1',\n    'DB_NAME' =\u003e 'dbname',\n    'DB_USER' =\u003e 'username',\n    'DB_PASSWORD' =\u003e 'password',\n    'NB_MAX_MYSQL_CLIENT' =\u003e 50,\n    'NB_MAX_PROMISE_IN_LOOP' =\u003e 50,\n    'DEFAULT_GENERATOR_LOCALE' =\u003e 'en_US'\n);\n```\nNB_MAX_MYSQL_CLIENT is the max number of MySQL connections simultaneously when executing your scripts.\nBy default, MySQL supports at most 151 connections simultaneously, but you can modify your MySQL variable 'max_connections' to break this restriction.\n\nNB_MAX_PROMISE_IN_LOOP is the max number of promises we keep in the promise table. Each promise represents the future result of an SQL query. The larger the number, the faster the execution will be. But you have to be careful that holding a large number of promises will consume too much memory and CPU resources. If your processor can't afford it, the run time will be at least 10 times longer than expected. \u003cstrong\u003eIf you don't know too much about the performance of your processor, just leave this variable to 50, or even 20 if you are not quite confident on it\u003c/strong\u003e.\n\nDEFAULT_GENERATOR_LOCALE influences the generated data's language and format by Faker's generator. You can find the full list of locales from [here](https://github.com/fzaninotto/Faker/tree/master/src/Faker/Provider)\n\n\n\n## Example code\n\n```php\n\u003c?php\n\nrequire './vendor/autoload.php';\nuse Globalis\\MysqlDataAnonymizer\\Anonymizer;\n\n$anonymizer = new Anonymizer();\n\n// Describe `users` table.\n$anonymizer-\u003etable('users', function ($table) {\n    \n    // Specify a primary key of the table. An array should be passed in for composite key.\n    $table-\u003eprimary('id');\n\n    // Add a global filter to the queries.\n    // Only string is accepted so you need to write down the complete WHERE statement here.\n    $table-\u003eglobalWhere('email4 != email5 AND id != 10');\n\n    // Replace with static data.\n    $table-\u003ecolumn('email1')-\u003ereplaceWith('john@example.com');\n\n    // Use #row# template to get \"email_0@example.com\", \"email_1@example.com\", \"email_2@example.com\"\n    $table-\u003ecolumn('email2')-\u003ereplaceWith('email_#row#@example.com');\n\n    // To replace with dynamic data a $generator is needed.\n    // By default, a fzaninotto/Faker generator will be used. \n    // Any generator object can be set like that - `$anonymizer-\u003esetGenerator($generator);`\n    $table-\u003ecolumn('email3')-\u003ereplaceWith(function ($generator) {\n        return $generator-\u003eemail;\n    });\n\n    // Use `where` to leave some data untouched for a specific column.\n    // If you don't list a column here, it will be left untouched too.\n    $table-\u003ecolumn('email4')-\u003ewhere('ID != 1')-\u003ereplaceWith(function ($generator) {\n        return $generator-\u003eunique()-\u003eemail;\n    });\n\n    // Use the values of current row to update a field\n    // This is a position sensitive operation, so the value of field 'email4' here is the updated value.\n    // So if you put this line before the previous one, the value of 'email4' here would be the valeu of 'email4' before update.\n    $table-\u003ecolumn('email5')-\u003ereplaceByFields(function ($rowData) {\n        return strtolower($rowData['email4']);\n    });\n\n    // Here we assume that there is a foreign key in the table 'class' on the column 'user_id'.\n    // To make sure 'user_id' get updated when we update 'id', use function 'synchronizeColumn'.\n    $table-\u003ecolumn('id')-\u003ereplaceWith(function ($generator) {\n        return $generator-\u003eunique()-\u003euuid;\n    })-\u003esynchronizeColumn(['user_id', 'class']);\n});\n\n$anonymizer-\u003erun();\n\necho 'Anonymization has been completed!';\n\n```\n\nFor more fake data types and details about fake data generator, you can find what you want from [fzaninotto/Faker's Github page](https://github.com/fzaninotto/Faker)\n\n\n## Helpers and providers\n\nYou can add your own helper and generator classes in src/helpers and src/providers. File names of helpers and providers need to keep these format : 'XXXHelper.php', 'XXXProvider.php', or they won't be loaded.\n\nAn example of customize helper:\n\n```php\n\u003c?php\n\nnamespace Globalis\\MysqlDataAnonymizer\\Helpers; //Default namespace, should always use this one\n\nclass StrHelper //Class name needs to be the same as file name\n{\n    public static function toLower($string)\n    {\n        return strtolower($string);\n    }\n}\n```\n\nThen in your script, you can use it like this:\n```php\n\u003c?php\n\nrequire './vendor/autoload.php';\nuse Globalis\\MysqlDataAnonymizer\\Anonymizer;\nuse Globalis\\MysqlDataAnonymizer\\Helpers;\n\n$anonymizer = new Anonymizer();\n\n$anonymizer-\u003etable('users', function ($table) {\n    \n    $table-\u003eprimary('id');\n    $table-\u003ecolumn('name')-\u003ereplaceByFields(function ($rowData, $generator) {\n        return Helpers\\StrHelper::toLower(($rowData['name']));\n    });\n}\n```\n\nAn example of customize provider:\n```php\n\u003c?php\n\nnamespace Globalis\\MysqlDataAnonymizer\\Provider; //Default namespace, should always use this one\n\nclass EnumProvider extends \\Faker\\Provider\\Base //Class name needs to be the same as file name, and provider classes need to extend \\Faker\\Provider\\Base\n{\n\n    //This simple method returns a fruit randomly from the list\n    public function fruit()\n    {\n        $enum = ['apple', 'orange', 'banana'];\n\n        return $enum[rand(0, 2)];\n    }\n}\n```\n\nThen in your script, you can use it like this:\n```php\n\u003c?php\n\nrequire './vendor/autoload.php';\nuse Globalis\\MysqlDataAnonymizer\\Anonymizer;\n\n$anonymizer = new Anonymizer();\n\n$anonymizer-\u003etable('users', function ($table) {\n    $table-\u003eprimary('id');\n    $table-\u003ecolumn('favorite_fruit')-\u003ereplaceWith(function ($generator) {\n        return $generator-\u003efruit;\n    });\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglobalis-ms%2Fmysql-data-anonymizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglobalis-ms%2Fmysql-data-anonymizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglobalis-ms%2Fmysql-data-anonymizer/lists"}