search
HomeDatabaseMysql TutorialMySQL迁移工具在生产环境下的使用

在产品迭代开发发布过程中,由于业务需求的增加,数据库难免会有结构调整等操作.在每个版本发布过程中怎么控制每个版本server端程

在产品迭代开发发布过程中,由于业务需求的增加,,数据库难免会有结构调整等操作.
在每个版本发布过程中怎么控制每个版本server端程序与数据库版本保持一致,以及数据库升级、回滚等操作.
本博文宅鸟将向大家推荐一款mysql数据库迁移工具mysql-php-migrations
由于具体需求不同,宅鸟根据自己公司的情况将mysql-php-migrations做了一些修改来满应用!

宅鸟修改程序后的mysql迁移程序有以下目录:

config 配置文件
dbscript sql脚本目录
lib 迁移程序类库
migrate.php 迁移命令执行入口


执行php migrate.php
可以看到如下结果

我们可以看到migrate.php有很多命令
php migrate.php add  test
结果:
      __ __        __      __
|\/|  (_ /  \|  __ |__)|__||__) __ |\/|. _  _ _ |_. _  _  _
|  |\/__)\_\/|__    |  |  ||      |  ||(_)| (_||_|(_)| )_)
  /                                    _/
******************************************************************** v2.0.1 ***


New migration created: file
/var/www/mysqlMigrations/dbscript/2013_12_18_14_50_45_test.php


*******************************************************************************
cd dbscript
total 16
-rw-r--r-- 1 www-data www-data 4837 Sep 29 09:21 2013_06_18_17_14_16_v1.php
-rw-r--r-- 1 www-data www-data  802 Sep 29 13:29 2013_09_29_12_00_12_v1.php
-rw-r--r-- 1 root    www-data  240 Dec 18 14:50 2013_12_18_14_50_45_test.php
此时dbscript目录已经新添加一个2013_12_18_14_50_45_test.php文件,改文件格式如下:
class Migration_2013_12_18_14_50_45 extends MpmMysqliMigration
{
      public function up(ExceptionalMysqli &$mysqli)
      {
              $mysqli->exec("DO 0");
      }
      public function down(ExceptionalMysqli &$mysqli)
      {
              $mysqli->exec("DO 0");
      }


}
?>

把需要修改的数据库脚本写在up函数中:
把对应修改修改所做的回滚操作卸载down函数中

注意:在生产环境下建议只做数据库的向上变迁,不做down操作,避免用户有用数据丢失.

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to install MySQL on Linux systemHow to install MySQL on Linux systemApr 29, 2025 pm 03:57 PM

Installing MySQL on Linux can be done through the package manager. The specific steps are as follows: 1. On Ubuntu, use apt to update the package list and install the MySQL server; 2. On CentOS, use yum to install the MySQL community version and start the service. After installation, basic configuration needs to be performed, such as setting the root password and creating database and users.

Creation and usage scenarios of MySQL viewsCreation and usage scenarios of MySQL viewsApr 29, 2025 pm 03:54 PM

MySQL views are virtual tables generated based on SQL queries. 1. Create a view: Use the CREATEVIEW statement combined with SELECT query. 2. Usage scenarios: simplify complex queries, data abstraction and ensure data consistency. 3. Optimization strategy: simplify underlying queries, use indexes, and consider materialized views.

Specify character sets and collations when creating databases in MySQLSpecify character sets and collations when creating databases in MySQLApr 29, 2025 pm 03:51 PM

When creating a database in MySQL, character sets and collation rules should be specified to ensure data accuracy and improve query performance. 1) Use the CREATEDATABASEmy_databaseCHARACTERSETutf8mb4COLLATIONutf8mb4_unicode_ci command to create a database, select the utf8mb4 character set and utf8mb4_unicode_ci collation. 2) utf8mb4 supports more Unicode characters, while utf8mb4_unicode_ci provides case-insensitive comparisons. 3) Ensure that the application layer and database layer use the same character set and collation rules to avoid potential problems.

How to sort and rank data in MySQLHow to sort and rank data in MySQLApr 29, 2025 pm 03:48 PM

In MySQL, sorting uses the ORDERBY clause, and ranking uses the RANK(), DENSE_RANK(), and ROW_NUMBER() functions. 1. Sort: Use ORDERBY clause, such as SELECT*FROMemployeesORDERBYsalaryDESC; 2. Ranking: Use window functions, such as SELECTemployee_name, salary, RANK()OVER(ORDERBYsalaryDESC)ASrankFROMemployees; these operations are based on SQL query optimizer and execution engine, and are often used to sort quickly or merge sort, and ranking depends on window function calculation.

Creation and calling methods of MySQL stored proceduresCreation and calling methods of MySQL stored proceduresApr 29, 2025 pm 03:45 PM

To create and call stored procedures in MySQL, follow the following steps: 1. Create stored procedures: Use the CREATEPROCEDURE statement to define stored procedures, including names, parameters, and SQL statements. 2. Compile stored procedures: MySQL compiles stored procedures into executable code and stores them. 3. Call stored procedure: use CALL statement and pass parameters. 4. Execute stored procedures: MySQL executes the SQL statements in it, processes parameters and returns the result.

How to set up MySQL service automatically startsHow to set up MySQL service automatically startsApr 29, 2025 pm 03:42 PM

The MySQL service can be set to automatically start on Windows, Linux, and macOS. 1) On Windows, use the command "scconfigmysqlstart=auto" to configure. 2) On Linux, enable it using "sudosystemctlenablemysql". 3) On macOS, create and load the launchd configuration file to achieve automatic startup.

How to view detailed structure information of MySQL tablesHow to view detailed structure information of MySQL tablesApr 29, 2025 pm 03:39 PM

The methods to view the MySQL table structure include: 1. Use the DESCRIBE command to view column information; 2. Use the SHOWCREATETABLE command to view table creation statements; 3. Use information_schema to query more detailed information. These methods help to quickly understand table structure and improve work efficiency.

Detailed explanation of the installation steps of MySQL on macOS systemDetailed explanation of the installation steps of MySQL on macOS systemApr 29, 2025 pm 03:36 PM

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment