能否用mysql在两个数据库之间传送文件? 遇到一个需求:需要在两个MySQL数据库之间进行数据库表的备份。在备份数据库表的同时,还要备份和这些表相关的一些文件。这些文件并不需要存储在数据库中。请问能不能通过SQL语句来实现文件的远程拷贝?非常感谢您花宝贵的时间来回答这个问题! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在不同的服务器上要用FEDERATED引擎CREATE TABLE xxx_b ( id INT(20) NOT NULL AUTO_INCREMENT, username VARCHAR(32) NOT NULL DEFAULT '', password VARCHAR(32), PRIMARY KEY (id) ) ENGINE=FEDERATED CONNECTION='mysql://broot:[email protected]:3306/dbname/b表'; 再备份,一般用MYSQLDUMP备份 数据库里存的是C:\1.JPG,你备份的时候希望把数据库和C:\1.JPG这个文件同时备份出去,是 这个意思吧?用SQL语句来操作外部文件,好像不能。两个库之间的备份直接用MYSQL的复制就OK。 通过SQL仅能实现一些表的数据的备份,并且要先创建FEDERATED引擎的连接针对与每个需要备份的表。SQL语句无法实现非数据库中的其它文件的备份。 文件备份你需要通过操作系统来完成。 建议你还是通过标准的MYSQL备份方案来备份数据。 然后通过脚本,比如FTP来备份你的文件。 我是MySQL的新手,请问能不能具体讲讲,或者给个link? 版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。刚刚抽空做了一下MYSQL 的主主同步。把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST1、环境描述。 主机:192.168.0.231(A) 主机:192.168.0.232(B) MYSQL 版本为5.1.212、授权用户。A:mysql> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)B:mysql> grant replication slave,file on *.* to 'repl2'@'192.168.0.231' identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)然后都停止MYSQL 服务器。3、配置文件。在两个机器上的my.cnf里面都开启二进制日志 。A:user = mysqllog-bin=mysql-binserver-id = 1binlog-do-db=testbinlog-ignore-db=mysqlreplicate-do-db=testreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allsync_binlog=1auto_increment_increment=2auto_increment_offset=1B:user = mysqllog-bin=mysql-binserver-id = 2binlog-do-db=testbinlog-ignore-db=mysqlreplicate-do-db=testreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allsync_binlog=1auto_increment_increment=2auto_increment_offset=2至于这些参数的说明具体看手册。红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。4、重新启动MYSQL服务器。在A和B上执行相同的步骤[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &[1] 4264[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'./usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data5、进入MYSQL的SHELL。A:mysql> flush tables with read lock\GQuery OK, 0 rows affected (0.00 sec)mysql> show master status\G*************************** 1. row *************************** File: mysql-bin.000007 Position: 528 Binlog_Do_DB: testBinlog_Ignore_DB: mysql1 row in set (0.00 sec)B:mysql> flush tables with read lock;Query OK, 0 rows affected (0.00 sec)mysql> show master status\G*************************** 1. row *************************** File: mysql-bin.000004 Position: 595 Binlog_Do_DB: testBinlog_Ignore_DB: mysql1 row in set (0.00 sec)然后备份自己的数据,保持两个机器的数据一致。方法很多。完了后看下一步。6、在各自机器上执行CHANGE MASTER TO命令。A:mysql> change master to -> master_host='192.168.0.232', -> master_user='repl2', -> master_password='123456', -> master_log_file='mysql-bin.000004', -> master_log_pos=595;Query OK, 0 rows affected (0.01 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)B:mysql> change master to -> master_host='192.168.0.231', -> master_user='repl1', -> master_password='123456', -> master_log_file='mysql-bin.000007', -> master_log_pos=528;Query OK, 0 rows affected (0.01 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)7、查看各自机器上的IO进程和 SLAVE进程是否都开启。A:mysql> show processlist\G*************************** 1. row *************************** Id: 2 User: repl Host: 192.168.0.232:54475 db: NULLCommand: Binlog Dump Time: 1590 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL*************************** 2. row *************************** Id: 3 User: system user Host: db: NULLCommand: Connect Time: 1350 State: Waiting for master to send event Info: NULL*************************** 3. row *************************** Id: 4 User: system user Host: db: NULLCommand: Connect Time: 1149 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL*************************** 4. row *************************** Id: 5 User: root Host: localhost db: testCommand: Query Time: 0 State: NULL Info: show processlist4 rows in set (0.00 sec)B:mysql> show processlist\G*************************** 1. row *************************** Id: 1 User: system user Host: db: NULLCommand: Connect Time: 2130 State: Waiting for master to send event Info: NULL*************************** 2. row *************************** Id: 2 User: system user Host: db: NULLCommand: Connect Time: 1223 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL*************************** 3. row *************************** Id: 4 User: root Host: localhost db: testCommand: Query Time: 0 State: NULL Info: show processlist*************************** 4. row *************************** Id: 5 User: repl2 Host: 192.168.0.231:50718 db: NULLCommand: Binlog Dump Time: 1398 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL4 rows in set (0.00 sec)如果红色部分没有出现,检查DATA目录下的错误文件。8、释放掉各自的锁,然后进行插数据测试。mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)插入之前两个机器表的对比:A:mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb | | t22 | +----------------+B:mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb | | t22 | +----------------+从A机器上进行插入A:mysql> create table t11_replicas -> (id int not null auto_increment primary key, -> str varchar(255) not null) engine myisam;Query OK, 0 rows affected (0.01 sec)mysql> insert into t11_replicas(str) values -> ('This is a master to master test table');Query OK, 1 row affected (0.01 sec)mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb | | t11_replicas | | t22 | +----------------+3 rows in set (0.00 sec)mysql> select * from t11_replicas;+----+---------------------------------------+| id | str |+----+---------------------------------------+| 1 | This is a master to master test table | +----+---------------------------------------+1 row in set (0.00 sec)现在来看B机器:mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb | | t11_replicas | | t22 | +----------------+3 rows in set (0.00 sec)mysql> select * from t11_replicas;+----+---------------------------------------+| id | str |+----+---------------------------------------+| 1 | This is a master to master test table | +----+---------------------------------------+1 row in set (0.00 sec)现在反过来从B机器上插入数据:B:mysql> insert into t11_replicas(str) values('This is a test 2');Query OK, 1 row affected (0.00 sec)mysql> select * from t11_replicas;+----+---------------------------------------+| id | str |+----+---------------------------------------+| 1 | This is a master to master test table | | 2 | This is a test 2 | +----+---------------------------------------+2 rows in set (0.00 sec)我们来看AA:mysql> select * from t11_replicas;+----+---------------------------------------+| id | str |+----+---------------------------------------+| 1 | This is a master to master test table | | 2 | This is a test 2 | +----+---------------------------------------+2 rows in set (0.00 sec)好了。现在两个表互相为MASTER。 mysql官方:http://dev.mysql.com/doc/refman/5.1/zh/index.html MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html 请问我能不能建一个临时表,把文件存储进去,备份到远程机器后再解压出来?如果这个方案可行,应当用哪些MySQL的技术去实现?谢谢! 是二进制的流文件,size>1M,请问您说的"代码导入、导出"是什么意思? 你的字段类型是BLOB之类的吧?MYSQL导出二进制文件利用into dumpfile。可以让MYSQL将BLOB类型的字段以二进制的形式存储到硬盘上。 这也是个方法。你可以使用 LOAD_FILE(file_name) 把文件存入BLOB字段,然后同步到本地计算机,再写回到文件。但需要有文件的权限。UPDATE t SET blob_col=LOAD_FILE('/tmp/picture') WHERE id=1; 谢谢~ 请问LOAD_FILE的函数说明怎么能看到? MySQL有没有类似MSDN的东东? http://dev.mysql.com/doc/refman/5.1/zh/index.html找找 MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html 谢谢各位的帮助!我正在下面这篇文章,跟我遇到的问题比较符合http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx 还有这篇Uploading Files To the Server Using PHPhttp://www.php-mysql-tutorial.com/wikis/php-tutorial/uploading-files-to-the-server-using-php.aspx 参考一下这个http://rit.com/rit/ 在 a 机器上 mysqldump -uroot -p yourdb table > table.sqlscp table.sql user@b机器:/home/youruser在 b 机器上 mysql -uroot -p yourdb< table.sql help:我要在官方网下载哪一个版本的mysql来安装? Mysql中如何将id大于某一值,如2,的所有记录的id,减1, 再写回数据库? 求sql语句,有难度的。谢谢 mysql 修复错误看不懂 请问索引问题 不能插入“㎡”这个字符 变量 pl/pgsql高手请进,急,先谢过了 mysql怎么建立数据源用jdbc.odbc 求算时间的MYSQL语句 为什么执行create table t1 like t2执行了55秒钟?求思路,求想法 MYSQL5,1 -------同一个存储过程内2个预处理语句,第二个不执行
CREATE TABLE xxx_b (
id INT(20) NOT NULL AUTO_INCREMENT,
username VARCHAR(32) NOT NULL DEFAULT '',
password VARCHAR(32),
PRIMARY KEY (id)
)
ENGINE=FEDERATED
CONNECTION='mysql://broot:[email protected]:3306/dbname/b表';
再备份,一般用MYSQLDUMP备份
SQL语句无法实现非数据库中的其它文件的备份。 文件备份你需要通过操作系统来完成。
刚刚抽空做了一下MYSQL 的主主同步。
把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST
1、环境描述。
主机:192.168.0.231(A)
主机:192.168.0.232(B)
MYSQL 版本为5.1.21
2、授权用户。
A:
mysql> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'repl2'@'192.168.0.231' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。3、配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql
log-bin=mysql-bin
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1B:
user = mysql
log-bin=mysql-bin
server-id = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
至于这些参数的说明具体看手册。
红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。
紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
4、重新启动MYSQL服务器。
在A和B上执行相同的步骤
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
[1] 4264
[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
/usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected
071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data5、进入MYSQL的SHELL。
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
-> master_host='192.168.0.232',
-> master_user='repl2',
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> change master to
-> master_host='192.168.0.231',
-> master_user='repl1',
-> master_password='123456',
-> master_log_file='mysql-bin.000007',
-> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)7、查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:mysql> show processlist\G
*************************** 1. row ***************************
Id: 2
User: repl
Host: 192.168.0.232:54475
db: NULL
Command: Binlog Dump
Time: 1590
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 2. row ***************************
Id: 3
User: system user
Host:
db: NULL
Command: Connect
Time: 1350
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 4
User: system user
Host:
db: NULL
Command: Connect
Time: 1149
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 4. row ***************************
Id: 5
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
4 rows in set (0.00 sec)B:mysql> show processlist\G
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 2130
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 1223
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 4. row ***************************
Id: 5
User: repl2
Host: 192.168.0.231:50718
db: NULL
Command: Binlog Dump
Time: 1398
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
4 rows in set (0.00 sec)如果红色部分没有出现,检查DATA目录下的错误文件。8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)插入之前两个机器表的对比:
A:mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
B:mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
-> (id int not null auto_increment primary key,
-> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.01 sec)mysql> insert into t11_replicas(str) values
-> ('This is a master to master test table');
Query OK, 1 row affected (0.01 sec)mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t11_replicas |
| t22 |
+----------------+
3 rows in set (0.00 sec)mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)
现在来看B机器:mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t11_replicas |
| t22 |
+----------------+
3 rows in set (0.00 sec)mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)现在反过来从B机器上插入数据:
B:mysql> insert into t11_replicas(str) values('This is a test 2');
Query OK, 1 row affected (0.00 sec)mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
| 2 | This is a test 2 |
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
| 2 | This is a test 2 |
+----+---------------------------------------+
2 rows in set (0.00 sec)好了。现在两个表互相为MASTER。
MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
如果这个方案可行,应当用哪些MySQL的技术去实现?谢谢!
是二进制的流文件,size>1M,请问您说的"代码导入、导出"是什么意思?
这也是个方法。
你可以使用 LOAD_FILE(file_name) 把文件存入BLOB字段,然后同步到本地计算机,再写回到文件。但需要有文件的权限。
UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;
谢谢~ 请问LOAD_FILE的函数说明怎么能看到? MySQL有没有类似MSDN的东东?
找找
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
http://www.php-mysql-tutorial.com/wikis/php-tutorial/uploading-files-to-the-server-using-php.aspx
http://rit.com/rit/
scp table.sql user@b机器:/home/youruser
在 b 机器上 mysql -uroot -p yourdb< table.sql