python指令碼完成資料庫的一個表一個表的遷移到另一臺伺服器
安裝MySQL-python
要想使python可以操作mysql 就需要MySQL-python驅動,它是python 操作mysql必不可少的模組。
下載MySQL-python-1.2.5.zip 檔案之後直接解壓。進入MySQL-python-1.2.5目錄:
>>python setup.py install
報錯:
[root@S220 MySQL-python-1.2.5]# python setup.py install
sh: mysql_config: command not found
Traceback (most recent call last):
File "setup.py", line 17, in
metadata, options = get_config()
File "/MySQL-python-1.2.5/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/MySQL-python-1.2.5/setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
解決辦法:手工安裝mysql_python
在setup_posix.py內找到mysql_config.path=/xxx/xxx(mysql安裝路徑/bin/mysql_config)
如下:
[root@S220 MySQL-python-1.2.5]# vi setup_posix.py
import os, sys
from ConfigParser import SafeConfigParser
# This dequote() business is required for some older versions
# of mysql_config
def dequote(s):
if s[0] in "\"'" and s[0] == s[-1]:
s = s[1:-1]
return s
def compiler_flag(f):
return "-%s" % f
def mysql_config(what):
from os import popen
f = popen("%s --%s" % (mysql_config.path, what))
data = f.read().strip().split()
ret = f.close()
if ret:
if ret/256:
data = []
if ret/256 > 1:
raise EnvironmentError("%s not found" % (mysql_config.path,))
return data
mysql_config.path ='/home/mysql/bin/mysql_config'
def get_config():
from setup_common import get_metadata_and_options, enabled, create_release_file
之後:
[root@S220 MySQL-python-1.2.5]# python setup.py install
running install
。
。
。
。
。
在包含自 _mysql.c:44 的檔案中:
/home/mysql/include/my_config.h:442:1: 警告:“HAVE_WCSCOLL”重定義
在包含自 /usr/local/include/python2.7/Python.h:8 的檔案中,
從 _mysql.c:29:
/usr/local/include/python2.7/pyconfig.h:902:1: 警告:這是先前定義的位置
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
至此MySQL-python驅動安裝成功。。。。。。
把mysql查出的內容放到一個列表中:
[root@S220 ~]# cat mysqldump.py
#!/usr/bin/python
#-*_ coding:UTF-8 _*_
import MySQLdb
conn= MySQLdb.connect(
host='192.168.0.144',
port = 3306,
user='root',
passwd='admin',
db ='jf',
)
cur = conn.cursor()
dbs=cur.execute('show databases')
mail_list=[]
#獲取所有結果
databases = cur.fetchall()
result=list(databases)
for r in result:
mail_list.append(r)
print mail_list
[root@S220 ~]# python mysqldump.py
[('ad',), ('agency',), ('beifen',), ('chinabidding',), ('crm',), ('crm2',), ('data',), ('dingyue',), ('experience',), ('fxb2016',), ('hdzq',), ('history',), ('info',), ('info_addition',), ('info_back',), ('information_schema',), ('infoservice',), ('jf',), ('jf2016',), ('liuwenhe',), ('log',), ('mailer',), ('mysql',), ('performance_schema',), ('resin_session',), ('sbiao',), ('sbw2',), ('stat',), ('test',), ('web',), ('weblog',), ('winfo',)]
上面的結果中是一個列表中又有一個元祖,這不是我們想要的結果,所以先迴圈列表再迴圈元祖,這樣可以真正做到把資料庫查出的內容存到一個列表中。如下紅色部分。
[root@S220 ~]# cat mysqldump.py
#!/usr/bin/python
#-*_ coding:UTF-8 _*_
import MySQLdb
conn= MySQLdb.connect(
host='192.168.0.144',
port = 3306,
user='root',
passwd='admin',
db ='jf',
)
cur = conn.cursor()
dbs=cur.execute('show databases')
mail_list=[]
#獲取所有結果
databases = cur.fetchall()
result=list(databases)
for r in result:
mail_list.append(r)
dbs_list=[]
for db in mail_list:
for db1 in db:
dbs_list.append(db1)
print(dbs_list)
下面是寫的一個python指令碼,用來完成從一臺mysql伺服器遷移到另一臺mysql伺服器,一個表一個表的遷移,這試用於搭建主從,或者搭建測試環境的場景。我這個未來便於理解,寫的有點多餘,其實不需要把取出來的庫和下面的表名字,放到一個列表中這2步驟。
[root@S220 ~]# cat mysqldump.py
#!/usr/bin/python
#-*_ coding:UTF-8 _*_
import MySQLdb
import os
conn= MySQLdb.connect(
host='192.168.0.144',
port = 3306,
user='root',
passwd='***',
db ='jf',
)
cur = conn.cursor()
dbs=cur.execute('show databases')
mail_list=[]
#獲取所有結果
databases = cur.fetchall()
result=list(databases)
for r in result:
mail_list.append(r)
dbs_list=[]
for db in mail_list:
for db1 in db:
dbs_list.append(db1)
conn.close()
cur.close()
tables_list=[]
for db2 in dbs_list:
if db2=='liuwenhe':
conn_name={"host":"192.168.0.**","user":"root","passwd":"ad****min","db":db2}
conn1= MySQLdb.connect(**conn_name)
cur_new = conn1.cursor()
tbs=cur_new.execute('show tables')
tbs_list=[]
tables = cur_new.fetchall()
result=list(tables)
for t in result:
tbs_list.append(t)
for tb1 in tbs_list:
for tb2 in tb1:
tables_list.append(tb2)
conn1.close()
cur_new.close()
for table in tables_list:
dumpcmd = "/home/mysql/bin/mysqldump -uroot " + " --opt " " -padmin " +" -h192.168.0.** " +"-d " +db2 +" "+table +" > " + "/backup/" + db2 +"_"+table+ ".sql"
os.system(dumpcmd)
sql1="/home/mysql/bin/mysql "+"-uroot "+"-pg****in@123 "+"-h192.168.0.220 "+db2 +"
os.system(sql1)
print "data translate completed"
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29654823/viewspace-2146753/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- rman將一個庫遷移到另一個伺服器上伺服器
- 將表從一個表空間遷移到另外一個表空間
- github倉庫遷移到gitlab以及gitlab倉庫遷移到另一個gitlab伺服器GithubGitlab伺服器
- Sql Server 匯入另一個資料庫中的表資料SQLServer資料庫
- Oracle如何把一個表匯出匯入到另一個伺服器上的另一個表裡Oracle伺服器
- SqlServer將資料庫中的表複製到另一個資料庫SQLServer資料庫
- 伺服器內的資料如何遷移到另一臺不同型號的伺服器上伺服器
- 外部表的另一種用途 資料遷移
- SQL Server一個表新增資料,引發另一個表更新資料 的一個簡單觸發器例子SQLServer觸發器
- 查詢一個表的一列插入到另一個表
- 通過 SQL Server 檢視訪問另一個資料庫伺服器表的方法SQLServer資料庫伺服器
- 把一個資料表中的資料匯入另一個表,觸發器和儲存過程的觸發器儲存過程
- 七牛資料從一個桶轉移到另一個桶
- 觸發器—一個表更新資料時同步欄位到另一個表中觸發器
- 比較兩個mysql資料庫裡面的表是否相同的一個校驗指令碼MySql資料庫指令碼
- [MySQL] - 聯表查詢,查詢一個不在另一個表的記錄MySql
- rman 資料庫從一臺機器遷移到另外的一臺機器資料庫
- EAS附件表由資料庫遷移到FTP資料庫FTP
- mysql 從一個表中查詢,插入到另一個表中MySql
- 一個命令,生成資料庫物件的指令碼資料庫物件指令碼
- Winform開發--資料從一個DataGridView轉移到另一個DataGridViewORMView
- MySQL 更新一個表裡的欄位等於另一個表某欄位的值MySql
- 使用exp/imp來移動表空間到另一個資料庫中的例子資料庫
- 【SQL 資料庫】將一張資料表資訊複製到另一張資料表SQL資料庫
- Sql查詢 一個表中某欄位的資料在另一個表中某欄位中不存在的SQL
- 一個shell 指令碼用來同步表用的指令碼
- 資料泵全庫遷移,排除一個大表【kingsql作品】SQL
- 定時從一個資料庫表中的資料儲存到另外一個資料庫中的表,而且怎麼處理重複的資料?...資料庫
- 把一個一中的欄位更新另一個表中的t-sqlSQL
- 批量的遷移一個資料庫上某使用者所有表的同義詞資料庫
- 一個表單中的資料新增到不同的資料表中
- Oracle imp 匯入資料到另一個表空間Oracle
- 兩個資料庫結構相同,但使用者、表空間名稱都不同,如何把一個資料庫中的使用者導到另一個資料庫
- 把表遷移到其他的表空間
- SSIS 開發篇-做一個簡單的SqlServer資料表資料遷移SQLServer
- 使用RMAN恢復一個資料庫到另一個目錄結構不同的資料庫中資料庫
- 刪除一個使用者下的所有表指令碼指令碼
- 動態SQL完成大表資料的遷移SQL