SSH备份和恢复MYSQL数据库

This method works regardless of the size of your database. You must have SSH access to your server. On (gs) plans you can invoke SSH access from within your Control Panel. On (dv) plans you must enable Shell Access through Plesk.

这个方法推荐给具有大数据库的家伙,而且你必须有SSH权限,

备份BACKUP:

1) Log into your server via SSH and cd into a directory where your user has write access. On (ss) plans
you would do something like this:

cd /var/www/html/

2) Enter the following command:

mysqldump –add-drop-table -u Username -p dbname > dbname.sql

- omit the ‘–add-drop-table’ argument if you’ll want to merge this backup with an existing database
upon restoral
- Where ‘Username’ is replaced by the mySQL username. On (ss) plans this user is the same as the
administrative FTP user. On (as) plans this user can be found by logging into Plesk, clicking
on the domain and going to databases and clicking on the database to be backed up.
- Replace ‘dbname’ with the name of the database to be backed up.
- Replace dbname.sql with what you’d like to name the backup.

3) Enter your mySQL password at the prompt. If you don’t know it you can reset it in your webcontrol or Plesk
admin panel 要输入正确的数据库密码,要是忘了就在控制面板里重设。
- If you get an error that looks like this:

ERROR 1045: Access denied for user: ‘Username@localhost’ (Using password: YES)

you have entered an incorrect password, please retype it carefully or reset it to something else via
the webcontrol panel or Plesk administrator

- On (ss) plans if you have reset the password in the webcontrol panel and are sure you’ve entered it correctly
on the command line this might be a permissions problem. Try renaming your database to something else in the
webcontrol panel and then back to the original, this often fixes permissions problems.

4) Use FTP to download the file to your backup location, probably your local computer.
- Don’t forget to delete the backup from your public html directory after you’ve found a safe place for it.
You don’t want to leave your backup lying around where anyone with a web browser can download a copy.

恢复RESTORE:

1) Use FTP to upload the file to your server, your public html directory will work for now
- Don’t forget to delete the backup from your public html directory after you’ve done the database restoral.
You don’t want to leave your backup lying around where anyone with a web browser can download a copy.
用FTP工具把数据库文件上传到你的public_html目录,别忘了删除这个备份,别人是可以下载到的。

2) Log into your server via SSH and cd to the directory where you’ve uploaded the file. On the (ss) plan if you
uploaded the backup into your public html directory you would use the command:

cd /var/www/html/

3) Enter the following command:

mysql -u Username -p dbname < dbname.sql

键入这行命令

- Where ‘Username’ is replaced by the mySQL username. Username处是你数据库管理员的名称,同你的FTP登录名。
On (ss) plans this user is the same as the
administrative FTP user. On (as) plans this user can be found by logging into Plesk, clicking
on the domain and going to databases and clicking on the database to be restored.
- Replace ‘dbname’ with the name of the database to be restored. 将被恢复的数据库名称
- Replace dbname.sql with the name of the backup.dbname.sql是备份数据库名称

If you have a zipped backup of your database you can use this line instead:如果是ZIP形式的,就用下面的命令。

gunzip < dbname.gz | mysql -u Username -p dbname

- Where ‘Username’ is replaced by the mySQL username. On (ss) plans this user is the same as the
administrative FTP user. On (as) plans this user can be found by logging into Plesk, clicking
on the domain and going to databases and clicking on the database to be restored.
- Replace ‘dbname’ with the name of the database to be restored.
- Replace dbname.gz with the name of the backup.

4) Enter your mySQL password at the prompt. If you don’t know it you can reset it in your webcontrol or Plesk
admin panel 然后输入密码。如果对了就大功告成,如果错了就出这个信息了。
- If you get an error that looks like this:

ERROR 1045: Access denied for user: ‘Username@localhost’ (Using password: YES)

you have entered an incorrect password, please retype it carefully or reset it to something else via
the webcontrol panel or Plesk administrator

这里只说text模式,binary模式先不提。

1.mysqldump备份database或table:

$mysqldump database [table1 table2] > dump.sql

比如备份数据库cm2:

$mysqldump cm2 >cm2.sql

或者只是cm2中的mail,user表:

$mysqldump cm2 mail usrer >cm2_mail_user.sql

mysqldump参数介绍

–databases: mysqldump后跟的所有都是database,而不是table。这个在需要备份多个数据库的时候有效。

–all-databases:备份所有的数据库的所有表到一个文件

另外这使用两个参数会自动在备份文件里添加判断database是否存在,并create database的命令。

而在默认的情况下没有此功能,如果原来的database没了,需要先创建database,才能导入。

2.使用mysql恢复数据库或表:

$mysql database < dump.sql

比如恢复之前备份的数据库cm2

$mysql cm2 < cm2.sql

如果没有使用--databases参数备份,那么必须指定数据库,并且该数据库存在。

如果使用了--databases参数,则可以不指定数据库,并且数据库可不存在。

所以一般情况备份database的情况下加上这个参数会比较好。

3.使用mysqldump和mysql拷贝database或者table

$mysqldump database [table] | mysql [-h other.host.com] database1

这里copy的前提是database1必须存在。

 

 

 

  使用mysqldump:
  
  (mysqldump命令位于mysql/bin/目录中)

  mysqldump工具很多方面类似相反作用的工具mysqlimport。它们有一些同样的选项。但mysqldump能够做更多的事情。它可以把整个数据库装载到一个单独的文本文件中。这个文件包含有所有重建您的数据库所需要的SQL命令。这个命令取得所有的模式(Schema,后面有解释)并且将其转换成DDL语法(CREATE语句,即数据库定义语句),取得所有的数据,并且从这些数据中创建INSERT语句。这个工具将您的数据库中所有的设计倒转。因为所有的东西都被包含到了一个文本文件中。这个文本文件可以用一个简单的批处理和一个合适SQL语句导回到MySQL中。这个工具令人难以置信地简单而快速。决不会有半点让人头疼地地方。
  
  因此,如果您像装载整个数据库Meet_A_Geek的内容到一个文件中,可以使用下面的命令:

  bin/mysqldump –p Meet_A_Geek > MeetAGeek_Dump_File.txt
  
  这个语句也允许您指定一个表进行dump(备份/导出/装载?)。如果您只是希望把数据库Meet_A_Geek中的表Orders中的整个内容导出到一个文件,可以使用下面的命令:

  bin/mysqldump –p Meet_A_Geek Orders >MeetAGeek_Orders.txt
  
  这个非常的灵活,您甚至可以使用WHERE从句来选择您需要的记录导出到文件中。要达到这样的目的,可以使用类似于下面的命令:

  bin/mysqldump –p –where="Order_ID > 2000" Meet_A_Geek Orders > Special_Dump.txt
  
  mysqldump工具有大量的选项,部分选项如下表:

  选项/Option 作用/Action Performed

  --add-drop-table

  这个选项将会在每一个表的前面加上DROP TABLE IF EXISTS语句,这样可以保证导回MySQL数据库的时候不会出错,因为每次导回的时候,都会首先检查表是否存在,存在就删除

  --add-locks

  这个选项会在INSERT语句中捆上一个LOCK TABLE和UNLOCK TABLE语句。这就防止在这些记录被再次导入数据库时其他用户对表进行的操作
  
  -c or - complete_insert

  这个选项使得mysqldump命令给每一个产生INSERT语句加上列(field)的名字。当把数据导出导另外一个数据库时这个选项很有用。

  --delayed-insert 在INSERT命令中加入DELAY选项

  -F or -flush-logs 使用这个选项,在执行导出之前将会刷新MySQL服务器的log.

  -f or -force 使用这个选项,即使有错误发生,仍然继续导出

  --full 这个选项把附加信息也加到CREATE TABLE的语句中

  -l or -lock-tables 使用这个选项,导出表的时候服务器将会给表加锁。

  -t or -no-create- info

  这个选项使的mysqldump命令不创建CREATE TABLE语句,这个选项在您只需要数据而不需要DDL(数据库定义语句)时很方便。
  
  -d or -no-data 这个选项使的mysqldump命令不创建INSERT语句。


在您只需要DDL语句时,可以使用这个选项。

  --opt 此选项将打开所有会提高文件导出速度和创造一个可以更快导入的文件的选项。

  -q or -quick 这个选项使得MySQL不会把整个导出的内容读入内存再执行导出,而是在读到的时候就写入导文件中。

  -T path or -tab = path 这个选项将会创建两个文件,一个文件包含DDL语句或者表创建语句,另一个文件包含数据。DDL文件被命名为table_name.sql,数据文件被命名为table_name.txt.路径名是存放这两个文件的目录。目录必须已经存在,并且命令的使用者有对文件的特权。
  
  -w "WHERE Clause" or -where = "Where clause "

  如前面所讲的,您可以使用这一选项来过筛选将要放到 导出文件的数据。
  
  假定您需要为一个表单中要用到的帐号建立一个文件,经理要看今年(2004年)所有的订单(Orders),它们并不对DDL感兴趣,并且需要文件有逗号分隔,因为这样就很容易导入到Excel中。 为了完成这个人物,您可以使用下面的句子:

  bin/mysqldump –p –where "Order_Date >='2000-01-01'"
  –tab = /home/mark –no-create-info –fields-terminated-by=, Meet_A_Geek Orders

  这将会得到您想要的结果。
  
  schema:模式

  The set of statements, expressed in data definition language, that completely describe the structure of a data base.

  一组以数据定义语言来表达的语句集,该语句集完整地描述了数据库的结构。
  
  SELECT INTO OUTFILE :

  如果您觉得mysqldump工具不够酷,就使用SELECT INTO OUTFILE吧, MySQL同样提供一个跟LOAD DATA INFILE命令有相反作用的命令,这就是SELECT INTO OUTFILE 命令,这两个命令有很多的相似之处。首先,它们有所有的选项几乎相同。现在您需要完成前面用mysqldump完成的功能,可以依照下面的步骤进行操作:
  
  1. 确保mysqld进程(服务)已经在运行

  2. cd /usr/local/mysql

  3. bin/mysqladmin ping ;// 如果这个句子通不过,可以用这个:mysqladmin -u root -p ping

  mysqladmin ping用于检测mysqld的状态,is alive说明正在运行,出错则可能需要用户名和密码。

  4. 启动MySQL 监听程序.

  5. bin/mysql –p Meet_A_Geek;// 进入mysql命令行,并且打开数据库Meet_A_Geek,需要输入密码

  6. 在命令行中,输入一下命令:

  SELECT * INTO OUTFILE '/home/mark/Orders.txt'
  FIELDS
  TERMINATED BY = ','
  FROM Orders
  WHERE Order_Date >= '2000-01-01'
  
  在你按了Return(回车)之后,文件就创建了。这个句子就像一个规则的SELECT语句,只是把想屏幕的输出重定向到了文件中。这意味这您可以使用JOIN来实现多表的高级查询。这个特点也可以被用作一个报表产生器。
  
  比方说,您可以组合这一章中讨论的方法来产生一个非常有趣的查询,试试这个:
  
  在mysql目录建立一个名为Report_G.rpt 的文本文件,加入下面的行:

  USE Meet_A_Geek;
  INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
  VALUES (NULL, "Kinnard", "Vicky");
  INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
  VALUES (NULL, "Kinnard", "Steven");
  INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
  VALUES (NULL, "Brown", "Sam");
  SELECT Last_Name INTO OUTFILE '/home/mark/Report.rpt'
  FROM Customers WHERE Customer_ID > 1;

  然后确认 mysql进程在运行,并且您在mysql目录中, 输入下面的命令:

  bin/mysql < Report_G.rpt检查您命名作为输出的文件,这个文件将会包含所有您在Customers表中输入的顾客的姓。 如您所见,您可以使用今天学到的导入/导出(import/export)的方法来帮助得到报表。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL数据库跨机备份是指将MySQL数据库备份文件从一台机器传输到另一台机器的过程。根据引用\[3\]中的内容,可以通过以下步骤进行MySQL数据库跨机备份: 1. 在源机器上使用mysqldump命令进行数据库备份,例如:`mysqldump -uroot -p'password' -A > /home/testAdmin/mysqldata-backup/all.sql`。 2. 在目标机器上创建目录,可以使用ssh命令进行远程操作,例如:`sshpass -p password ssh -p port testAdmin@targetIP "\[ -d /home/testAdmin/mysqldata \] && echo ok || mkdir -p /home/testAdmin/mysqldata"`。 3. 将备份文件从源机器传输到目标机器,可以使用scp命令进行远程传输,例如:`sshpass -p password scp -P port /usr/local/mysql/bin/all.sql testAdmin@targetIP:/home/testAdmin`。 以上步骤可以实现将MySQL数据库备份文件从源机器传输到目标机器,从而实现跨机备份。 #### 引用[.reference_title] - *1* *2* [Mysql数据库实现数据定时备份](https://blog.csdn.net/weixin_43464964/article/details/129996420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySQL数据库跨机器备份](https://blog.csdn.net/qq_37358328/article/details/125778590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值