操作流程
前言:近期做了一个项目,由于mysql数据库中的数据比较庞大,考虑转储成sql文件迁移数据比较耗时,我们想到了另一种办法,即直接复制data文件夹下的所有文件以及文件夹到另一台服务器。假设我们是把数据文件从服务器A迁移到服务器B(要保证两台服务器的mysql版本一致,不然会遇到许多未知的问题)
获取数据
想要得到我们需要的数据,就要知道mysql的数据文件位于哪一个目录下:首先进入数据库,然后我们可以用下面的命令获取数据文件位置:
mysql -u root -p#进入数据库命令
mysql> show variables like '%dir%';#查看数据库数据文件位于哪一个目录下

可以看出,我们的数据文件位于/var/lib/mysql/文件夹下,接着我们可以把这个文件夹下面的内容打包压缩拷贝出来:
tar -czvf mysql.tar.gz mysql#将mysql数据文件全部打包成mysql.tar.gz
mv mysql.tar.gz 目标文件夹/文件名#将你打包的数据文件移动到你需要的目录并且拷贝
迁移数据
把数据文件从A服务器拷贝到B服务器后,找到B服务器存放数据文件的目录,把A服务器的数据解压拷贝到B服务器的数据文件目录。
注意:把目录下所有的文件以及文件夹迁移过来后,一定要保证ibdata1的存在,删除ib_logfile0和ib_logfile1文件。
tar -xzvf mysql.tar.gz /var/lib/#解压数据文件到对应的文件夹下面
运行新的数据库
我们迁移数据成功后,可能会遇到很多问题,这时不要怕,出错了我们可以查看出错日志:
cat /var/log/mysql/error.log#在这里可以看到我们启动数据库时错误的原因
如果是权限问题,我们可以用下面的命令解决:
chmod -R 777 /mysql#设置该目录下所有的文件权限
设置完权限后,我们可以启动数据库了
service mysql start#启动数据库,启动完之后,我们就可以进入新的数据库,这时的密码就不是原来数据库的密码了,是新数据库的密码
后记
- 重启数据库服务时,我遇到了权限问题报错如下:
解决方式就是重新设置该目录的权限。
mysql 迁移数据文件之后报 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:03.127681Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-11-29T01:39:03.127829Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:03.127867Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 21236 ...
2020-11-29T01:39:03.130277Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:03.130295Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:03.148672Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-11-29T01:39:03.148715Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-11-29T01:39:03.148722Z 0 [Note] InnoDB: Uses event mutexes
2020-11-29T01:39:03.148727Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2020-11-29T01:39:03.148732Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-11-29T01:39:03.148737Z 0 [Note] InnoDB: Using Linux native AIO
2020-11-29T01:39:03.149605Z 0 [Note] InnoDB: Number of pools: 1
2020-11-29T01:39:03.158912Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-11-29T01:39:03.161199Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-11-29T01:39:03.175956Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-11-29T01:39:03.178432Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-11-29T01:39:03.188551Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:03.188605Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:03.188617Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-11-29T01:39:03.789570Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-11-29T01:39:03.789631Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-11-29T01:39:03.789648Z 0 [ERROR] Failed to initialize builtin plugins.
2020-11-29T01:39:03.789659Z 0 [ERROR] Aborting
2020-11-29T01:39:03.789722Z 0 [Note] Binlog end
2020-11-29T01:39:03.789834Z 0 [Note] Shutting down plugin 'CSV'
2020-11-29T01:39:03.790789Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2020-11-29T01:39:04.320755Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-11-29T01:39:04.320891Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:04.320926Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 21292 ...
2020-11-29T01:39:04.323460Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:04.323478Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:04.325871Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-11-29T01:39:04.325898Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-11-29T01:39:04.325903Z 0 [Note] InnoDB: Uses event mutexes
2020-11-29T01:39:04.325907Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2020-11-29T01:39:04.325911Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-11-29T01:39:04.325915Z 0 [Note] InnoDB: Using Linux native AIO
2020-11-29T01:39:04.326127Z 0 [Note] InnoDB: Number of pools: 1
2020-11-29T01:39:04.326218Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-11-29T01:39:04.327710Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-11-29T01:39:04.335408Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-11-29T01:39:04.337134Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-11-29T01:39:04.347237Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:04.347292Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:04.347300Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-11-29T01:39:04.948228Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-11-29T01:39:04.948290Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-11-29T01:39:04.948307Z 0 [ERROR] Failed to initialize builtin plugins.
2020-11-29T01:39:04.948319Z 0 [ERROR] Aborting
2020-11-29T01:39:04.948380Z 0 [Note] Binlog end
2020-11-29T01:39:04.948537Z 0 [Note] Shutting down plugin 'CSV'
2020-11-29T01:39:04.949484Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2020-11-29T01:39:05.567996Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-11-29T01:39:05.568116Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:05.568147Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 21340 ...
2020-11-29T01:39:05.570398Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:05.570416Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:05.572609Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-11-29T01:39:05.572636Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-11-29T01:39:05.572640Z 0 [Note] InnoDB: Uses event mutexes
2020-11-29T01:39:05.572644Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2020-11-29T01:39:05.572647Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-11-29T01:39:05.572651Z 0 [Note] InnoDB: Using Linux native AIO
2020-11-29T01:39:05.572839Z 0 [Note] InnoDB: Number of pools: 1
2020-11-29T01:39:05.572919Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-11-29T01:39:05.574426Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-11-29T01:39:05.581404Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-11-29T01:39:05.583049Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-11-29T01:39:05.593142Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:05.593181Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:05.593189Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-11-29T01:39:06.193807Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-11-29T01:39:06.193839Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-11-29T01:39:06.193847Z 0 [ERROR] Failed to initialize builtin plugins.
2020-11-29T01:39:06.193852Z 0 [ERROR] Aborting
2020-11-29T01:39:06.193872Z 0 [Note] Binlog end
2020-11-29T01:39:06.193923Z 0 [Note] Shutting down plugin 'CSV'
2020-11-29T01:39:06.194256Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2020-11-29T01:39:06.566904Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-11-29T01:39:06.567024Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:06.567055Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 21401 ...
2020-11-29T01:39:06.569375Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:06.569394Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:06.571552Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-11-29T01:39:06.571577Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-11-29T01:39:06.571581Z 0 [Note] InnoDB: Uses event mutexes
2020-11-29T01:39:06.571585Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2020-11-29T01:39:06.571588Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-11-29T01:39:06.571600Z 0 [Note] InnoDB: Using Linux native AIO
2020-11-29T01:39:06.571790Z 0 [Note] InnoDB: Number of pools: 1
2020-11-29T01:39:06.571868Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-11-29T01:39:06.573424Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-11-29T01:39:06.581070Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-11-29T01:39:06.582785Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-11-29T01:39:06.592919Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:06.592966Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-11-29T01:39:06.592975Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-11-29T01:39:07.193808Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-11-29T01:39:07.193880Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-11-29T01:39:07.193897Z 0 [ERROR] Failed to initialize builtin plugins.
2020-11-29T01:39:07.193907Z 0 [ERROR] Aborting
2020-11-29T01:39:07.193949Z 0 [Note] Binlog end
2020-11-29T01:39:07.194058Z 0 [Note] Shutting down plugin 'CSV'
2020-11-29T01:39:07.195001Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2020-11-29T01:39:07.825922Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-11-29T01:39:07.826069Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:07.826108Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 21457 ...
2020-11-29T01:39:07.828835Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:07.828858Z 0 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
2020-11-29T01:39:07.831298Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-11-29T01:39:07.831329Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-11-29T01:39:07.831334Z 0 [Note] InnoDB: Uses event mutexes
2020-11-29T01:39:07.831338Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2020-11-29T01:39:07.831341Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-11-29T01:39:07.831345Z 0 [Note] InnoDB: Using Linux native AIO
2020-11-29T01:39:07.831558Z 0 [Note] InnoDB: Number of pools: 1
2020-11-29T01:39:07.831648Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-11-29T01:39:07.833391Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2

本文介绍了如何在Linux系统中,将MySQL的data文件夹从服务器A迁移至服务器B,以快速转移大量数据。迁移过程包括确定数据路径、打包拷贝、解压覆盖,并处理权限及启动数据库。这种方法适用于数据库版本相同的情况,需要注意保留关键文件如ibdata1,避免丢失ib_logfile文件。
最低0.47元/天 解锁文章

557

被折叠的 条评论
为什么被折叠?



