Problem Solution Discussion Copying Tables or Databases to Another Server

10.17 Copying Tables or Databases to Another Server

10.17.1 Problem

You want t o copy t ables or dat abases from one MySQL server t o anot her.

10.17.2 Solution

Use m ysqldum p and m ysql t oget her, connect ed by a pipe.

10.17.3 Discussion

SQL- form at out put from m ysqldum p can be used t o copy t ables or dat abases from one server t o anot her. Suppose you want t o copy t he states t able from t he cookbook dat abase on t he local host t o t he cb dat abase on t he host ot her- host .com . One w ay t o do t his is t o dum p t he out put int o a file as described in Recipe 10.16 : mysqldump cookbook states dump.txt Then copy dum p.t xt t o ot her- host .com and run t he following com m and t here t o im port t he t able int o t hat servers cb dat abase: mysql cb dump.txt Anot her way t o accom plish t his wit hout using an int erm ediary file is t o send t he out put of m ysqldum p direct ly over t he net work t o t he rem ot e MySQL server. I f you can connect t o bot h servers from t he host w here t he cookbook dat abase resides, use t his com m and: mysqldump cookbook states | mysql -h other-host.com cb The m ysqldum p half of t he com m and connect s t o t he local server and writ es t he dum p out put t o t he pipe. The m ysql half of t he com m and connect s t o t he rem ot e MySQL server on ot her- host .com . I t reads t he pipe for input and sends each st at em ent t o t he ot her- host .com server. I f you cannot connect direct ly t o t he rem ot e server using m ysql from your local host , send t he dum p out put int o a pipe t hat uses ssh t o invoke m ysql rem ot ely on ot her- host .com : mysqldump cookbook states | ssh other-host.com mysql cb ssh connect s t o ot her- host .com and launches m ysql t here. Then it reads t he m ysqldum p out put from t he pipe and passes it t o t he rem ot e m ysql process. Using ssh can be useful when you want t o send a dum p over t he net work t o a m achine t hat has t he MySQL port blocked by a firewall but t hat allows connect ions on t he SSH port . I f you dont have access t o ssh, you m ay be able t o use rsh inst ead. However, rsh is insecure, so ssh is m uch preferred. To copy m ult iple t ables over t he net work, nam e t hem all following t he dat abase argum ent of t he m ysqldum p com m and. To copy an ent ire dat abase, dont specify any t able nam es aft er t he dat abase nam e. m ysqldum p will dum p all t he t ables cont ained in t he dat abase. I f youre t hinking about invoking m ysqldum p wit h t he - - all- dat abases opt ion t o send all your dat abases t o anot her server, consider t hat t he out put will include t he t ables in t he mysql dat abase t hat cont ains t he grant t ables. I f t he rem ot e server has a different user populat ion, you probably dont want t o replace t hat servers grant t ables

10.18 Writing Your Own Export Programs