Mysql 升级 + Python 数据库备份

[复制链接]
查看11 | 回复2 | 2012-11-9 16:31:15 | 显示全部楼层 |阅读模式
本帖最后由 归一一 于 2012-11-25 18:51 编辑
MYSQL升级
之前从来没想过要更新数据库,平时用着挺稳定的。
前段时间看 很多说升级,升级特别快,也没什么感触。
直至前两天发现,不是最新版本很多功能没法用,比如分区。 正好要做主从备份。两台服务器要同版本;
目前版本:Server version: 5.0.95 需要升级到:5.5.28
系统是centos ,理论上 用 yum update mysql也可以, 但由于 yum源的问题。 只能更新到 5.0.95;
下面步骤 参考网址: http://mushme.iteye.com/blog/1631855
1、安装MySQL 5.5.x的yum源:
rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm
2、安装MySQL客户端的支持包:
yum install libmysqlclient15 --enablerepo=webtatic
3、卸载MySQL老版本的软件包:
---------------------------------------在这里 为了防止数据库数据丢失;要备份整个数据库;
yum remove mysql mysql-*
4、安装MySQL 5.5的客户端和服务端:
yum install mysql55 mysql55-server --enablerepo=webtatic
5、启动MySQL系统服务,更新数据库:
/etc/init.d/mysqld restart
mysql_upgrade
升级完毕

要备份整个数据库;

因为有很多数据库,不想一个个备份。一时没想太多,顺便熟悉下python ,下面把python 备份 每个数据库 脚本贴出来;移植性很方便。

#/home/guiyi/DataBaseDump.py
#Filename: DataBaseDump.py
#connectDB 数据库连接,ExportSData 数据库全部数据写入文件,DumpData 备份每个数据库
#This program is developped by Adair, [email protected]. Updated on 11, 13, 2012.
import MySQLdb;
import time;
import os,sys;
OutFile = open( "temp.log.txt","a&quot

;
os.system("screen&quot

;
def connectDB():
global conn, cursor
try:
conn = MySQLdb.connect(host='localhost',user='***',db='***',passwd='***');
except Exception, e:
print e
sys.exit()
cursor = conn.cursor();
def ExportSData():
global conn, cursor
connectDB()
OutFile.write(" start ExportData runing ..."+"\t" + time.asctime().replace(" ",".&quot

+ "\n&quot

;
sql = "show databases;"
print sql;
cursor.execute(sql)
data = cursor.fetchall()
#print data;
outfile = open( "temp.S.file.txt","w&quot

;

for k in range(0,len(data)):
print data[k][0]
outfile.write(str(data[k][0])+"\n&quot

;
OutFile.write(" DoneExport-S-Dataruning ..."+"\t" + time.asctime().replace(" ",".&quot

+ "\n&quot

;
outfile.close();
def DumpData():
global conn, cursor
connectDB()
dumpfile = open( "temp.dump.file.txt","w&quot

;
readfile = open( "temp.S.file.txt&quot

;

while 1:
line = readfile.readline();
num1 = line[0:50].strip();

#printnum1;
comm =" mysqldump -uroot -pjexresearch_pass "+ num1 + "|gzip>"+ num1 +".20121113.sql.gz";
print comm;
dumpfile.write(comm+"\n");

os.system(comm);
OutFile.write(" Done "+" comm"+"runing ..."+"\t" + time.asctime().replace(" ",".") + "\n");

if not line: break;
readfile.close();
dumpfile.close();
OutFile.close();
ExportSData();
DumpData();






回复

使用道具 举报

千问 | 2012-11-9 16:31:15 | 显示全部楼层
本帖最后由 归一一 于 2012-11-25 18:50 编辑
mysql-----主从复制
怎么升级mysql数据库为同版本,请参考上一篇。只说它的主从复制,步骤如下:
1、主从服务器分别作以下操作:
两台服务器 192.168.11.19 (从) 192.168.11.20 (主)
1.1、版本一致
1.2、初始化表,并在后台启动mysql
1.3、修改root的密码
2、修改主服务器master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=20 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=19 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
4、重启两台服务器的mysql
service mysqld restart |/etc/init.d/mysql restart
5、在主服务器上建立帐户并授权slave:

mysql>GRANT REPLICATION SLAVE ON *.* to 'backup'@'192.168.11.19' identified by '123456';//一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.11.19,加强安全。
6、登录主服务器的mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File
| Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |107 |
| mysql
|
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
7、配置从服务器Slave:
mysql>change master to master_host='192.168.11.20',master_user='backup',master_password='123456', master_log_file='mysql-bin.000007',master_log_pos=107;//注意不要断开,"107”无单引号。
Mysql>start slave;//启动从服务器复制功能
8、检查从服务器复制功能状态:
mysql> show slave status\G;
*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.11.20

Master_User: backup

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 107

Relay_Log_File: mysqld-relay-bin.000002

Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000007

Slave_IO_Running: Yes//此状态必须YES

Slave_SQL_Running: Yes //此状态必须YES

Replicate_Do_DB:

Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 107

Relay_Log_Space: 410

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:
Replicate_Ignore_Server_Ids:

Master_Server_Id: 20


注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database b;
Query OK, 1 row affected (0.00 sec)
mysql> use b;
Database changed
mysql> create table a1(id int);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into a1 values (1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a1 values (2);
Query OK, 1 row affected (0.01 sec)
mysql> show tables;
+-------------+
| Tables_in_b |
+-------------+
| a1
|
+-------------+
1 row in set (0.00 sec)
从服务器:
mysql> use b;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------+
| Tables_in_b |
+-------------+
| a1
|
+-------------+
1 row in set (0.00 sec)
mysql> select * from a1;
+------+
| id |
+------+
|1 |
|2 |
+------+
2 rows in set (0.00 sec)


回复

使用道具 举报

千问 | 2012-11-9 16:31:15 | 显示全部楼层
哥顶的不是帖,是寂寞,留下的是美德。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行