设为首页 收藏本站
查看: 1863|回复: 0

[经验分享] 给python类动态添加方法(method)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-10 09:22:54 | 显示全部楼层 |阅读模式
windows8.1系统下配置mysql主从复制 1.测试环境概述 本地win8操作系统,64位操作系统,3G内存,下载的是mysql5.6.16-winx64.zip包 在本地磁盘上解压两个mysql,端口分别是3310和3311,即: localhost:3310 --master服务器 localhost:3311 --slave服务器 \
加载中... 2.配置master 将压缩包解压: C:/software/mysql-5.6.16-winx64-3310 在这个目录下建立tmp文件夹 在该目录下新增my.ini文件,其内容如下:

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/ ... ation-defaults.html # *** DO NOT EDIT THIS FILE. It"s a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] server-id=1 #replicate-same-server-id=0 # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = C:/software/mysql-5.6.16-winx64-3310 datadir = C:/software/mysql-5.6.16-winx64-3310/data tmpdir = C:/software/mysql-5.6.16-winx64-3310/tmp port = 3310 log-bin=master-bin log-bin-index=master-bin.index #default-character-set = utf8 # server_id = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] port = 3310 #default-character-set = utf8

其中,server-id变量表示该节点在集群内的唯一表示,每个节点必须使用不同的server-id来区分 basedir,datadir,tmpdir这些表示的是数据文件或临时文件的存放路径,因为我们在一台机器上创建2个mysql数据库服务,为了两个实例之间的数据不发生干扰,我们在配置文件中明确指定其位置 port:这个参数就是mysql服务的监听端口,这里我们设置的是3310 log-bin:要将服务器设置为master,需要确保该服务器有一个活动的二进制日志(binary log),该日志上保留了服务器上的所有改变,并且这些改变可以在slave上被重新执行 3.启动master 命令行切换到 C:\software\mysql-5.6.16-winx64-3310\bin(master解压目录),执行mysqld --console命令,得如下信息,则master启动完毕:

    C:\software\mysql-5.6.16-winx64-3310\bin>mysqld --console 2014-03-26 22:20:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-03-26 22:20:48 7204 [Note] Plugin 'FEDERATED' is disabled. 2014-03-26 22:20:48 7204 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-03-26 22:20:48 7204 [Note] InnoDB: The InnoDB memory heap is disabled 2014-03-26 22:20:48 7204 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions 2014-03-26 22:20:48 7204 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-03-26 22:20:48 7204 [Note] InnoDB: Not using CPU crc32 instructions 2014-03-26 22:20:48 7204 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-03-26 22:20:48 7204 [Note] InnoDB: Completed initialization of buffer pool 2014-03-26 22:20:48 7204 [Note] InnoDB: Highest supported file format is Barracuda. 2014-03-26 22:20:48 7204 [Note] InnoDB: 128 rollback segment(s) are active. 2014-03-26 22:20:48 7204 [Note] InnoDB: Waiting for purge to start 2014-03-26 22:20:48 7204 [Note] InnoDB: 5.6.16 started; log sequence number 1625977 2014-03-26 22:20:49 7204 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d440636c-b4f1-11e3-9ec4-904ce5e379cc. 2014-03-26 22:20:49 7204 [Note] Server hostname (bind-address): '*'; port: 3310 2014-03-26 22:20:49 7204 [Note] IPv6 is available. 2014-03-26 22:20:49 7204 [Note] - '::' resolves to '::'; 2014-03-26 22:20:49 7204 [Note] Server socket created on IP: '::'. 2014-03-26 22:20:50 7204 [Note] Event Scheduler: Loaded 0 events 2014-03-26 22:20:50 7204 [Note] mysqld: ready for connections. Version: '5.6.16-log' socket: '' port: 3310 MySQL Community Server (GPL)

4.在master上创建一个复制用户 4.1修改默认的root密码 如果是已有的数据库则该步骤,则修改密码部分省略 C:/software/mysql-5.6.16-winx64-3310\mysqladmin -uroot password 111111 通过如上命令,我们给默认的root帐号一个密码111111 4.2添加复制用户并给予权限: C:/software/mysql-5.6.16-winx64-3310 \mysql -uroot -p111111 进入到mysql数据库中

    mysql> create user repl_user; Query OK, 0 rows affected (0.00 sec) mysql> mysql> grant replication slave on *.* to repl_user identified by '111111'; Query OK, 0 rows affected (0.00 sec) mysql>

replication slave这个权限没有什么特别之处,只是这个用户可以从master上取得二进制的日志转储数据,完全可以给一个常规帐号赋予replication slave权限,但是最好还是将replication slave权限和其他用户权限分开,这样的话,如果后期想禁止某些slave的链接,只要删除该用户就可以了 5.配置slave 将压缩包解压: C:/software/mysql-5.6.16-winx64-3311 在这个目录下建立tmp文件夹 在该目录下新增my.ini文件,其内容如下:

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/ ... ation-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] server-id=2 #replicate-same-server-id=0 # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = C:/software/mysql-5.6.16-winx64-3311 datadir = C:/software/mysql-5.6.16-winx64-3311/data tmpdir = C:/software/mysql-5.6.16-winx64-3311/tmp port = 3311 relay-log = slave-relay-bin relay-log-index = slave-relay-bin.index #default-character-set = utf8 # server_id = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] port = 3311 #default-character-set = utf8

与master一样,slave也要配置一个唯一的服务器id,relay-log和relay-log-index表示中继日志文件和中继日志索引文件 6.启动slave 命令行切换到 C:\software\mysql-5.6.16-winx64-3311\bin(slave解压目录),执行mysqld --console命令,得如下信息,则slave启动完毕:

    C:\software\mysql-5.6.16-winx64-3311\bin>mysqld --console 2014-03-26 22:21:19 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-03-26 22:21:19 6916 [Note] Plugin 'FEDERATED' is disabled. 2014-03-26 22:21:19 6916 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-03-26 22:21:19 6916 [Note] InnoDB: The InnoDB memory heap is disabled 2014-03-26 22:21:19 6916 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions 2014-03-26 22:21:19 6916 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-03-26 22:21:19 6916 [Note] InnoDB: Not using CPU crc32 instructions 2014-03-26 22:21:19 6916 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-03-26 22:21:19 6916 [Note] InnoDB: Completed initialization of buffer pool 2014-03-26 22:21:19 6916 [Note] InnoDB: Highest supported file format is Barracuda. 2014-03-26 22:21:20 6916 [Note] InnoDB: 128 rollback segment(s) are active. 2014-03-26 22:21:20 6916 [Note] InnoDB: Waiting for purge to start 2014-03-26 22:21:20 6916 [Note] InnoDB: 5.6.16 started; log sequence number 1625977 2014-03-26 22:21:20 6916 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e6f98904-b4f1-11e3-9ec5-904ce5e379cc. 2014-03-26 22:21:20 6916 [Note] Server hostname (bind-address): '*'; port: 3311 2014-03-26 22:21:20 6916 [Note] IPv6 is available. 2014-03-26 22:21:20 6916 [Note] - '::' resolves to '::'; 2014-03-26 22:21:20 6916 [Note] Server socket created on IP: '::'. 2014-03-26 22:21:21 6916 [Note] Event Scheduler: Loaded 0 events 2014-03-26 22:21:21 6916 [Note] mysqld: ready for connections. Version: '5.6.16' socket: '' port: 3311 MySQL Community Server (GPL)

7.将slave连接到master上 现在创建基本的复制只剩最后一步了:将slave指向master,让它知道从哪里进行复制,为此你需要知道master的4个信息:主机名,主机端口号,主机数据库用户,主机数据库帐号密码,因为创建master的时候已经创建了一个复制用户,因此在slave节点上执行如下指令:

    mysql> change master to master_host='localhost',master_port=3310,master_user='repl_user',master_password='111111'; Query OK, 0 rows affected, 2 warnings (0.45 sec) mysql>

    mysql> start slave; Query OK, 0 rows affected (0.05 sec) mysql> 群里有人问如何做到


def foo():
    pass

class Bar(object):
    pass

Bar.set_instance_method(foo)

b = Bar()
b.foo()



这个其实还是比较简单的, 只要写个函数给类设置属性即可, 可根据需求是否用函数包装下, 或者用staticmethod这个decorator:


import functools


def foo():
    print 'hello world'


class Bar(object):
    def __init__(self):
        self.data = 42

    @classmethod
    def set_instance_method(cls, func):
        @functools.wraps(func)
        def dummy(self, *args, **kwargs):
            func(*args, **kwargs)
        setattr(cls, func.func_name, dummy)

Bar.set_instance_method(foo)


b = Bar()
b.foo()
print b.foo
print Bar.foo



输出
1
2
3
   
hello world
<bound method Bar.foo of <__main__.Bar object at 0x10d41c890>>
<unbound method Bar.foo>



然后又问如果要这样做怎么写:

def foo():
    pass

class Bar(object):
    set_instance_method(foo)

这样问题就在于set_instance_method运行时如何获得类(Bar), 无奈, 尝试了下,最多也只能得到"Bar"(通过inspect模块).
不是很熟悉python内部原理, 只知道在Bar定义时Bar并不存在, 所以无法这么做.



不过不太清楚问问题的人思路是怎么样的, 如果纯粹是为了研究倒也还好.
如果把代码写成这样, 觉得没什么太多好处, 再加上可能增加了维护成本, 导致维护的人找不到函数的定义.

再者, 你让IDE怎么办.基本只能跑一遍代码才能正确解析了....

8.常见问题排查 执行如上命令后,输出如下信息: 2014-03-26 23:11:23 6916 [ERROR] Slave I/O: error connecting to master 'repl_user@localhost:3310' - retry-time: 60 retries: 1, Error_code: 1045 这个说明slave和master之间的链接没有建立成功,这个时候多半是添加了新用户repl_user后,没有生效导致 在master上执行flush privileges;来将新建的用户及权限生效


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-21913-1-1.html 上篇帖子: Eclipse配置python环境 下篇帖子: python 删除list中重复元素
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表