【问题标题】:Magento change database table prefixMagento 更改数据库表前缀
【发布时间】:2013-01-26 22:10:22
【问题描述】:

我不小心安装了一个没有下划线作为表前缀的 Magento。是否有自动更改此选项的选项?我不喜欢手动更改 337 个表格 :-)

我尝试了this 解决方案,但这似乎不起作用。

迈克尔

【问题讨论】:

  • 所选答案对您有用吗?什么版本的 Magento?

标签: php mysql magento


【解决方案1】:

您可以使用此选择创建 sql 来重命名所有表:

SELECT 'rename table '||table_name||' to '||'newprefix'||table_name||';'
FROM information_schema.tables

【讨论】:

  • 或将SELECT CONCAT('rename table ', table_name , ' to ', 'newprefix_', table_name, ';') FROM information_schema.tables 用于better compatibility
【解决方案2】:
<?php
$database_host="localhost";
$database_user="root";
$database_password="";
$magento_database="test1";
$table_prefix="magtest_"; 
?>
<?php
$db=mysql_connect($database_host,$database_user,$database_password);
mysql_select_db($magento_database);
$query="SHOW TABLES";
$result=mysql_query($query) or die('Err');
while($row=mysql_fetch_array($result)){$old_table=$row[0];
if(preg_match('/'.$table_prefix.'/',$old_table)){echo"Table $old_table already done<br/>\n";continue;} 
$new_table=$table_prefix.$old_table;echo"Renaming $old_table to $new_table<br/>\n";
$query="RENAME TABLE `$old_table`  TO `$new_table`";mysql_query($query);} 
?>

步骤:

  1. 进行数据库备份。
  2. 创建一个名为“rename_table_prefix.php”的文件并放在根目录下。
  3. 将上面的脚本粘贴进去。
  4. 运行文件http:www.domain.com/magento/rename_table_prefix.php
  5. 所有表都将被重命名。
  6. 转到 app/etc/local.xml
  7. 按原样修改以下行
  8. 大功告成。

【讨论】:

    【解决方案3】:

    我运行这个 php 脚本来更改 Magento DB 表前缀

    // mege_rename_table_prefix.php
    //New Prefix Name
    $table_prefix = "test_";
    //Magento Database Backup php script
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('display_errors', 1);
        ini_set('memory_limit', '1512M');
        // Get Magento Application
        require_once 'app/Mage.php';
        Mage::app();
      // Mage::app('default');
       //Mage::app('main');
        // get Magento config
        $config  = Mage::getConfig()->getResourceConnectionConfig("default_setup");
        $dbinfo = array(
            "host" => $config->host,
            "user" => $config->username,
            "pass" => $config->password,
            "dbname" => $config->dbname
        );
        // Database Config
        $db_host = $dbinfo["host"];
        $db_user = $dbinfo["user"];
        $db_pass = $dbinfo["pass"];
        $db_name = $dbinfo["dbname"];
    //conect db
    $db = mysql_connect($db_host, $db_user, $db_pass);
    mysql_select_db($db_name);
    $query = "SHOW TABLES";
    $result = mysql_query($query) or die('Err');
    while($row = mysql_fetch_array($result)) {
        $old_table = $row[0];
        if(preg_match('/'.$table_prefix.'/', $old_table)) {
            echo "Table $old_table already done<br/>\n";
            continue;
        }
        $new_table = $table_prefix.$old_table;
        echo "Renaming $old_table to $new_table<br/>\n";
        $query = "RENAME TABLE `$old_table`  TO `$new_table`";
        mysql_query($query);
        }
    

    【讨论】:

      【解决方案4】:

      您可以在 PHPMyAdmin 中简单地更改它。

      Click/open the database.
      
      Click Structure at the top bar.
      This will display all your tables. Note the existing prefix.
      
      Scroll to the bottom, to the last table.
      
      Click "Check all".
      This will check all tables.
      
      Click the drop down menu just next to it - the one with the default value "with selected".
      
      Select "Replace table prefix:"
      This will bring you to a new page with two text inputs.
      
      Fill in your existing prefix, e.g. "oldPrefi_". Don't forget the underscore.
      
      Fill in your new prefix, e.g. "newPrefi_". Don't forget the underscore.
      
      Finally, click submit.
      

      您将被重定向到带有新前缀的表格列表。

      【讨论】:

        【解决方案5】:

        第 1 步 - 更改 env.php 文件中的 table_prefix,如下面的屏幕截图所示

        第 2 步 - 运行以下查询以获取 ALTER 语句以将前缀添加到现有表 -:

        SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO qa_',
        TABLE_NAME, ';') FROM information_schema.tables WHERE table_schema =
        'DB_NAME'
        

        第 3 步 - 通过在 Magento 根文件夹上运行以下命令来升级 Magento 2 数据库

        php bin/magento setup:upgrade
        

        如果出现问题,您可以从 env.php 中删除 table_prefix 并在您的数据库实例上运行以下查询以将表重命名为原始表

        SELECT Concat('ALTER TABLE  ', TABLE_NAME ,  '  RENAME TO ',
        replace(TABLE_NAME,'qa_',''), ';') FROM information_schema.tables
        WHERE table_schema = 'magento_2'
        

        希望对某人有所帮助!

        【讨论】:

          【解决方案6】:

          解决该问题的步骤(100% 可行,我有同样的问题,我已解决)

          上述解决方案大多行不通,因为您可以 UPDATE 表名,但是 FORIEGN 键映射会导致问题。所以下面是解决这个问题的最佳方法。

          1. 导出整个数据库(备份)
          2. 从整个文件中删除 PREFIX 词。
          3. 删除现有数据库并导入更新后的数据库。
          4. 从 env.php 文件中删除 PREFIX

          【讨论】:

            猜你喜欢
            • 2019-01-16
            • 2019-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多