Safeguarding MySQL password when developing in Python?

前端 未结 4 2184
慢半拍i
慢半拍i 2021-02-05 15:41

I\'m writing a Python script which uses a MySQL database, which is locally hosted. The program will be delivered as source code. As a result, the MySQL password will be visible

4条回答
  •  自闭症患者
    2021-02-05 16:33

    In this case, I create a new section in my .my.cnf, like

    [files]
    host=127.0.0.1
    port=3307
    database=files
    default-character-set=utf8
    password=foobar
    

    and use it on DB initialization with

    d=MySQLdb.connect(
        read_default_group='files',
        port=0,  # read from .my.cnf
        db='files',
        cursorclass=cursors.DictCursor,
        # amongst other stuff
    )
    

提交回复
热议问题