我有一个WordPress多站点,需要从该站点提取信息。我编写了一个存储过程,它递归地为某种帖子构建树关系。现在我想知道如何将存储过程推送到所有站点的数据库中。(由于这是一种多站点配置,因此每个站点(本例中的子域)都有一个单独的数据库。)
请在这方面帮助我。
WordPress版本:3.5.1多站点配置类型:子域
//code needs refinement
DELIMITER //
CREATE PROCEDURE getPath(IN post_id INT, IN return_path TEXT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a INT;
DECLARE b TEXT;
DECLARE cur1 CURSOR FOR (SELECT * FROM TempTable112);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TABLE IF EXISTS TempTable112;
SET max_sp_recursion_depth := 10;
CREATE TEMPORARY TABLE TempTable112 AS SELECT id,post_title FROM wp_101_posts WHERE post_parent = post_id and post_status=\'publish\';
SET @s = CONCAT(\'SELECT * INTO OUTFILE \',"\'", CONCAT(\'Sample\',FLOOR(1000+RAND()*9999)),\'.txt\',"\'",\' FROM TempTable112\');
PREPARE stmt2 FROM @s;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a,b;
IF done = 1 THEN
LEAVE read_loop;
END IF;
SET return_path = CONCAT(\'/\',return_path,b);
CALL getPath(a, return_path);
END LOOP;
CLOSE cur1;
END //
DELIMITER ;
最合适的回答,由SO网友:RolandoMySQLDBA 整理而成
以下是步骤:
步骤01:将代码放入文本文件
打开一个文本编辑器(vi、nano、emacs)并将代码放在其中。
另存为/tmp/mynewcode.sql
步骤02:收集所有数据库名称
MYSQLCONN=`-uroot -ppassword`
SQLSTMT="SELECT schema_name FROM information_schema.schemata"
SQLSTMT="${SQLSTMT} WHERE schema_name NOT IN"
SQLSTMT="${SQLSTMT} (\'information_schema\',\'performance_schema\',\'mysql\')"
mysql ${MYSQLCONN} -ANe"${SQLSTMT}" > /tmp/dbnames.txt
步骤03:将存储过程加载到中提到的每个数据库中
/tmp/dbnames.txt
for DB in `cat /tmp/dbnames.txt` ; do mysql ${MYSQLCONN} -D${DB} < /tmp/mynewcode.sql ; done
步骤04:删除您创建的文件(可选)
rm -f /tmp/dbnames.txt
rm -f /tmp/mynewcode.sql
就是这样。
试试看!!!