Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1841542
  • 博文数量: 117
  • 博客积分: 2559
  • 博客等级: 少校
  • 技术积分: 4385
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-13 20:08
个人简介

作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.

文章分类

全部博文(117)

文章存档

2014年(1)

2013年(25)

2012年(13)

2011年(77)

2010年(1)

分类: Mysql/postgreSQL

2012-10-18 14:17:53

需求分析:
  1. root密码在多个地方出现过,比如分享的技术文档,邮件,截图.
  2. MySQL默认安装的管理员帐号名称root,众所周知.为了增强安全性,需要更换一个用户名称,例如换成superuser,或者有公司特色的.例如xxx_admin.
应对策略:
  1. 首先创建一个与root用户权限一样的用户.
    GRANT ALL PRIVILEGES ON *.* TO 'x_admin'@'127.0.0.1' IDENTIFIED BY 'xxxx';
  2. 删除默认的root用户.
    drop user root@'127.0.0.1';
    drop user root@'localhost';
    drop user root@'::1';
注意问题:
  1. 视图
    曾经用root帐号为DEFINER的视图,如果将root删除,将提示该视图无法使用,没有权限.所以要注意提前查看是否存在视图,存在的话,需要修改该视图的DEFINER属性.
    修改视图,是瞬间就能完成的操作,除非该视图被其他sql语句占用,处于锁定的状态.

    1. 查看视图
    2. select TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION, DEFINER from information_schema.VIEWS;

    3. 修改视图(非root的暂不修改)
    4. ALTER DEFINER=`x_admin`@`127.0.0.1` SQL SECURITY DEFINER VIEW v_name AS ...

  2. 存储过程/函数
    情况与视图类似

    1. 查看存储过程/视图
    2. select ROUTINE_SCHEMA,ROUTINE_NAME,ROUTINE_TYPE,DEFINER from information_schema.ROUTINES;
    3. 或者
    4. select db,name,type,definer from mysql.proc;


    5. 修改存储例程,可直接修改mysql.proc
    6. update mysql.proc set definer='x_admin@127.0.0.1' where db='db_name';
    7. 如果修改所有库
    8. update mysql.proc set definer='x_admin@127.0.0.1' ;

  3. 用root用户连接MySQL的脚本
    此类问题比较好解决,可单独为脚本创建帐号用来执行脚本中指定的操作,该用户名可用script_,或者脚本名命名.权限够用就行,不要分配过多的权限.
  4. 方法:一个增加用户的脚本.(配合批量执行)

    1. #!/usr/bin/python
    2. #-*- coding: UTF-8 -*-
    3. # ########################################################################
    4. # This program
    5. # Version: 2.0.0 (2012-10-10)
    6. # Authors: lianjie.ning@qunar.com
    7. # History:
    8. # ########################################################################

    9. import os
    10. import socket
    11. import subprocess
    12. import sys
    13. import traceback
    14. from ConfigParser import ConfigParser

    15. class Finger(object):
    16.     'finger.py'

    17.     def __init__ (self):
    18.         print '---- %s, %s' % (socket.gethostname(), self.__doc__)
    19.         
    20.     def load_config (self, file="finger.ini"):
    21.         if not os.path.exists(file):
    22.             print file,"is not exists, but is created, please fix it"
    23.             temp_ini = '''[conn_db]
    24. login_pwd = 
    25. exec_sql = 
    26. '''
    27.             open(file, 'w').write(temp_ini)
    28.             os.chmod(file, 0600)
    29.             sys.exit()
    30.         config = ConfigParser()
    31.         config.read(file)
    32.         if config.has_section('conn_db') is True:
    33.             if config.has_option('conn_db', 'login_pwd') is True:
    34.                 login_pwd = config.get('conn_db', 'login_pwd')
    35.             if config.has_option('conn_db', 'exec_sql') is True:
    36.                 exec_sql = config.get('conn_db', 'exec_sql')
    37.             return (login_pwd, exec_sql)
    38.    
    39.     def grant_user(self, login_pwd, exec_sql):
    40.         if os.path.exists('/usr/local/bin/mysql'):
    41.             mysql = '/usr/local/bin/mysql'
    42.         elif os.path.exists('/usr/bin/mysql'):
    43.             mysql = '/usr/bin/mysql'
    44.         elif os.path.exists('/bin/mysql'):
    45.             mysql = '/bin/mysql'
    46.         else:
    47.             print "command not fount of mysql"
    48.             sys.exit()

    49.         user = 'xxxx'
    50.         conn_port = [3306,3307,3308,3309,3310]
    51.         for i in conn_port:
    52.             ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    53.             address = ('127.0.0.1', int(i))
    54.             status = ss.connect_ex(address)
    55.             ss.settimeout(3)
    56.             ss.close()
    57.             if status == 0:
    58.                 conn_mysql  = '%s -u%s -p%s -h127.0.0.1 -P%d -N -s -e"%s"' % (mysql, user, login_pwd, i, exec_sql)
    59.                 p = subprocess.call(conn_mysql, shell=True, stdout=open("/dev/null"))
    60.                 if p == 0:
    61.                     print "---- checking port: %s is NORMAL" % i
    62.                 else:
    63.                     print "---- checking prot: %s is ERROR" % i

    64. if __name__ == '__main__':
    65.     try:
    66.         process = Finger()
    67.         (login_pwd, exec_sql) = process.load_config()
    68.         process.grant_user(login_pwd, exec_sql)
    69.     except Exception, e:
    70.         print str(e)
    71.         traceback.print_exc()
    72.         sys.exit()
阅读(10865) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~