将多个数据库移动到新服务器的最快方法

将多个数据库移动到新服务器的最快方法

我有几个中小型 mysql 数据库,大约 40 个,我需要将它们从一台具有不同 cpanel 帐户的 whm 服务器迁移到另一台具有从以前的服务器设置的帐户和我正在移动的数据库的旧版本的服务器上。

有人能推荐最快的方法吗,我本来打算手动转储每一个并导入,但这似乎非常耗时,如果可能的话,我想把我的机器作为中间人,并尽可能实现自动化。

答案1

我不太了解 cPanel,但我知道如何将数据库快速传输到另一台服务器 - 如果您可以访问 ssh。

使用带有适当参数的 mysqldump 并将其与 ssh 链接起来。因此数据库被导入,而源数据库仍被导出。没有使用临时文件(内部的 mysql 除外;))

源服务器# mysqldump --user=user1 --all-databases | ssh 目标主机'mysql --user=user2'

如果您使用私钥和 ssh-agent 向源服务器进行身份验证,则可以使用 ssh 的 -A 选项进行连接。因此您无需关心目标端的授权。但请记住:

         Agent forwarding should be enabled with caution.  Users with the
         ability to bypass file permissions on the remote host (for the
         agent's Unix-domain socket) can access the local agent through
         the forwarded connection.  An attacker cannot obtain key material
         from the agent, however they can perform operations on the keys
         that enable them to authenticate using the identities loaded into
         the agent.

(来源:man 1 ssh)

希望这个对你有帮助

答案2

可能有点太多了,因为@krissi 的答案非常好,但万一你需要多次执行,你可以使用这样的脚本:

#!/bin/bash
# MySQL databases migration script
# Jorge Barnaby (jorge {dot} barnaby {at} gmail)

################################################################################
# Configuration variables

ORIG_USER="origin-username"
ORIG_PASS="origin-password"
ORIG_HOST="origin-server"

DEST_USER="destination-username"
DEST_PASS="destination-password"
DEST_HOST="destination-server"

# Do not backup the following databases
IGNORED_DBS="information_schema"

################################################################################
# Start of the program

# Command that runs on the origin server to extract the databases
MYSQL_ORIG="mysqldump -u $ORIG_USER -h $ORIG_HOST -p$ORIG_PASS --add-drop-database --databases"

# Command that runs on the destination server to popuplate the databases
MYSQL_DEST="mysql -u $DEST_USER -h $DEST_HOST -p$DEST_PASS"

# Get all database list first
DBS="$(mysql -u $ORIG_USER -h $ORIG_HOST -p$ORIG_PASS -Bse 'show databases')"

echo
echo -----------------------------------------------------------
echo `date +"%F %T %Z"` : Starting MySQL Migration script
echo -----------------------------------------------------------
echo
echo -- MySQL Origin Server: $ORIG_HOST
echo -- MySQL Destination Server: $DEST_HOST

for db in $DBS
do
    skipdb=-1
    if [ "$IGNORED_DBS" != "" ];
    then
        for i in $IGNORED_DBS
        do
            [ "$db" == "$i" ] && skipdb=1 || :
        done
    fi

    if [ "$skipdb" == "-1" ];
    then
        echo
        echo -- `date +"%F %T %Z"` : Migrating database $db
        # Command to be executed piping mysqldump on the origin and mysql on the remote
        $MYSQL_ORIG $db | $MYSQL_DEST
        echo -- `date +"%F %T %Z"` : Done
    fi
done

echo
echo -----------------------------------------------------------
echo `date +"%F %T %Z"` : All Done
echo -----------------------------------------------------------

exit 0

相关内容