Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55533
  • 博文数量: 8
  • 博客积分: 218
  • 博客等级: 入伍新兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 11:20
文章分类

全部博文(8)

文章存档

2014年(1)

2012年(7)

我的朋友

分类: Mysql/postgreSQL

2012-05-08 10:53:09


点击(此处)折叠或打开

  1. #!/bin/sh
  2. #文 件 名: autogen_mysql.sh
  3. #功 能: 自动生成Mysql集群配置文件,生成环境自动部署,自动运行脚本等
  4. #创建时间: 2012-02-05
  5. #自动化生成的文件列表:
  6. #(执行autogen_mysql.sh后,会生成autogen目录,里面会存放自动生成的相关文件)
  7. #1. ConfigAllServer.sh     自动配置所有Mysql集群节点脚本,包括NDB节点,mysql节点,管理节点
  8. #2. RunAllServer.sh        自动启动所有Mysql集群节点脚本,包括NDB节点,mysql节点,管理节点
  9. #3. mgm_node.sh         启动,关闭,重启管理节点脚本.在执行ConfigAllServer.sh时,会将该脚本自动拷贝到管理节点的/etc/目录下
  10. #4. mysql_node.sh         启动或关闭Mysql节点脚本.在执行ConfigAllServer.sh时,会将该脚本自动拷贝到Mysql节点的/etc/目录下
  11. #5. ndb_node_start.sh     启动NDB节点脚本.在执行ConfigAllServer.sh时,会将该脚本自动拷贝到NDB节点的/etc/目录下
  12. #6. config.ini             管理配置文件.在执行ConfigAllServer.sh时,会将该配置文件自动拷贝到管理节点的/var/lib/mysql/目录下
  13. #7. my_ndb.cnf             NDB节点配置文件.在执行ConfigAllServer.sh时,会将该脚本自动拷贝到NDB节点的/etc/目录下,并命名为my.cnf
  14. #8. my_mysql.cnf         Mysql节点配置脚本.在执行ConfigAllServer.sh时,会将该脚本自动拷贝到Mysql节点的/etc/目录下,并命名为my.cnf
  15. #注意事项
  16. #1. 执行ConfigAllServer.sh, RunAllServer.sh这两个脚本的主机需要修改/etc/ssh/ssh_config,将StrictHostKeyChecking设置为no,防止弹出提示框引起自动化无法执行完毕
  17. # 测试 sysbench --mysql-user=uProfile --mysql-password=123456 --test=oltp --mysql-host=10.74.213.38 --oltp-test-mode=complex --mysql-table-engine=ndbcluster --oltp-table-size=5000000 --mysql-db=uProfile --num-threads=64 --max-requests=500000 prepare

  18. # -------------------------------------------------以下根据实际情况进行设定----------------------------------------------------
  19. # ROOT权限密码,该密码为所有集群节点通用密码,所有服务器ROOT将使用统一密码,便于自动化控制,否则上面提到的ConfigAllServer.sh, RunAllServer.sh这两个文件无法正常运行
  20. ROOTPASSWORD=123qwe
  21. # 管理节点IP地址
  22. MGM_IP=192.168.254.5
  23. # NDB节点IP数组,数据之间用空格隔开,'10.0.0.1 10.0.0.2'
  24. NDB_IPS='192.168.254.101 192.168.254.102'
  25. # MySQL节点IP数组,数据之间用空格隔开,'10.0.0.1 10.0.0.2'
  26. SQL_IPS='192.168.254.10 192.168.254.11'
  27. #--------------------------------------------------------------------------------------------------------------------------------
  28. rm -fr ./autogen_mysql
  29. mkdir ./autogen_mysql

  30. #Create my_ndb.cnf
  31. cat > ./autogen_mysql/my_ndb.cnf << end
  32. [mysqld]
  33. max_connections = 2000
  34. slow_query_log = /var/lib/mysql/data/slow_query.log
  35. long_query_time = 1
  36. ndbcluster
  37. ndb-connectstring=$MGM_IP

  38. [mysql_cluster]
  39. ndb-connectstring=$MGM_IP
  40. end

  41. #Create my_mysql.cnf
  42. cat > ./autogen_mysql/my_mysql.cnf << end
  43. [mysqld]
  44. ndbcluster
  45. ndb-connectstring=$MGM_IP:1186
  46. max_connections=2000
  47. skip-name-resolve
  48. #tmp_table_size=1024M
  49. #max_heap_table_size=1024M

  50. [mysql_cluster]
  51. ndb-connectstring=$MGM_IP:1186
  52. end

  53. id=10
  54. NDB_LIST=
  55. for ndb_ip in $NDB_IPS;do
  56.     NDB_LIST=$NDB_LIST"[NDBD]"$'\n'
  57.     NDB_LIST=$NDB_LIST"NodeId=$id"$'\n'
  58.     NDB_LIST=$NDB_LIST"HostName=$ndb_ip"$'\n'
  59.     id=$(expr $id + 1)
  60. done

  61. id=50
  62. SQL_LIST=
  63. for sql_ip in $SQL_IPS;do
  64.     SQL_LIST=$SQL_LIST"[MYSQLD]"$'\n'
  65.     SQL_LIST=$SQL_LIST"NodeId=$id"$'\n'
  66.     SQL_LIST=$SQL_LIST"HostName=$sql_ip"$'\n'
  67.     id=$(expr $id + 1)
  68. done

  69. #Create config.ini
  70. cat > ./autogen_mysql/config.ini << end
  71. [NDBD DEFAULT]
  72. NoOfReplicas: 2
  73. DataDir: /home/Mysql/Data
  74. backupdatadir: /home/Mysql/Backup
  75. FileSystemPath: /home/Mysql/File
  76. # Data Memory, Index Memory, and String Memory #
  77. DataMemory: 5120M
  78. IndexMemory: 512M
  79. StringMemory: 5
  80. # Transaction Parameters #
  81. MaxNoOfConcurrentTransactions: 40960
  82. MaxNoOfConcurrentOperations: 100000
  83. MaxNoOfLocalOperations: 100000
  84. # Transaction Temporary Storage #
  85. MaxNoOfConcurrentIndexOperations: 8192
  86. MaxNoOfFiredTriggers: 4000
  87. TransactionBufferMemory: 1M
  88. # Scans and buffering #
  89. MaxNoOfConcurrentScans: 300
  90. MaxNoOfLocalScans: 1000
  91. BatchSizePerLocalScan: 64
  92. LongMessageBuffer: 1M
  93. # Logging and Checkpointing #
  94. NoOfFragmentLogFiles: 300
  95. FragmentLogFileSize: 16M
  96. MaxNoOfOpenFiles: 40
  97. InitialNoOfOpenFiles: 27
  98. MaxNoOfSavedMessages: 25
  99. # Metadata Objects #
  100. MaxNoOfAttributes: 1500
  101. MaxNoOfTables: 400
  102. MaxNoOfOrderedIndexes: 200
  103. MaxNoOfUniqueHashIndexes: 200
  104. MaxNoOfTriggers: 770
  105. # Boolean Parameters #
  106. LockPagesInMainMemory: 0
  107. StopOnError: 1
  108. Diskless: 0
  109. ODirect: 0
  110. # Controlling Timeouts, Intervals, and Disk Paging #
  111. TimeBetweenWatchDogCheck: 6000
  112. TimeBetweenWatchDogCheckInitial: 6000
  113. StartPartialTimeout: 30000
  114. StartPartitionedTimeout: 60000
  115. StartFailureTimeout: 1000000
  116. HeartbeatIntervalDbDb: 5000
  117. HeartbeatIntervalDbApi: 5000
  118. TimeBetweenLocalCheckpoints: 20
  119. TimeBetweenGlobalCheckpoints: 2000
  120. TransactionInactiveTimeout: 0
  121. TransactionDeadlockDetectionTimeout: 1200
  122. DiskSyncSize: 4M
  123. DiskCheckpointSpeed: 10M
  124. DiskCheckpointSpeedInRestart: 100M
  125. ArbitrationTimeout: 10
  126. # Buffering and Logging #
  127. UndoIndexBuffer: 2M
  128. UndoDataBuffer: 1M
  129. RedoBuffer: 32M
  130. LogLevelStartup: 15
  131. LogLevelShutdown: 3
  132. LogLevelStatistic: 0
  133. LogLevelCheckpoint: 0
  134. LogLevelNodeRestart: 0
  135. LogLevelConnection: 0
  136. LogLevelError: 15
  137. LogLevelCongestion: 0
  138. LogLevelInfo: 3
  139. MemReportFrequency: 0
  140. # Backup Parameters #
  141. BackupDataBufferSize: 2M
  142. BackupLogBufferSize: 2M
  143. BackupMemory: 64M
  144. BackupWriteSize: 32K
  145. BackupMaxWriteSize: 256K

  146. [TCP DEFAULT]
  147. SendBufferMemory=10M
  148. ReceiveBufferMemory=2M

  149. [NDB_MGMD DEFAULT]
  150. PortNumber=1186
  151. Datadir=/home/Mysql/Data

  152. [NDB_MGMD]
  153. NodeId=1
  154. HostName=$MGM_IP

  155. $NDB_LIST
  156. $SQL_LIST

  157. [MYSQLD]
  158. [MYSQLD]
  159. [MYSQLD]
  160. [MYSQLD]
  161. [MYSQLD]
  162. end

  163. #Create ndb_node_start.sh
  164. cat > ./autogen_mysql/ndb_node_start.sh << end
  165. #!/bin/sh
  166. service iptables stop
  167. chkconfig iptables off

  168. ndbd &

  169. if ! grep ndb_node_start /etc/rc.local > /dev/null
  170. then
  171.     echo "sh /etc/ndb_node_start.sh" >> /etc/rc.local
  172. fi

  173. end

  174. #Create mysql_node.sh
  175. cat > ./autogen_mysql/mysql_node.sh << end
  176. #!/bin/sh
  177. service iptables stop
  178. chkconfig iptables off
  179. case "\$1" in
  180.     start)
  181.         mysqld_safe &
  182.         ;;
  183.     stop)
  184.         mysqladmin -uroot -p$ROOTPASSWORD shutdown
  185.         ;;
  186.     *)
  187.         echo "Usage: \$0 {start|stop}"
  188.         exit 1
  189. esac

  190. if ! grep mysql_node /etc/rc.local > /dev/null
  191. then
  192.         echo "sh /etc/mysql_node.sh start" >> /etc/rc.local
  193. fi

  194. end

  195. #Create mgm_node.sh
  196. cat > ./autogen_mysql/mgm_node.sh << end
  197. #!/bin/sh
  198. service iptables stop
  199. chkconfig iptables off
  200. case "\$1" in
  201.     start)
  202.         ndb_mgmd -f /var/lib/mysql/config.ini --configdir=/var/lib/mysql/
  203.         ;;
  204.     stop)
  205.         ndb_mgm -e shutdown
  206.         ;;
  207.     restart)
  208.         ndb_mgm -e shutdown
  209.         ndb_mgmd -f /var/lib/mysql/config.ini --configdir=/var/lib/mysql/ --reload
  210.         ;;
  211.     *)
  212.         echo "Usage: \$0 {start|stop|restart}"
  213.         exit 1
  214. esac

  215. if ! grep mgm_node /etc/rc.local > /dev/null
  216. then
  217.         echo "sh /etc/mgm_node.sh start" >> /etc/rc.local
  218. fi

  219. end

  220. #Create ConfigAllServer.sh
  221. SCP_LIST=
  222. for ndb_ip in $NDB_IPS;do
  223.     SCP_LIST=$SCP_LIST"spawn scp my_ndb.cnf root@$ndb_ip:/etc/my.cnf"$'\n'
  224.     SCP_LIST=$SCP_LIST"expect \"*password*\""$'\n'
  225.     SCP_LIST=$SCP_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  226.     SCP_LIST=$SCP_LIST"expect eof"$'\n'$'\n'
  227.     
  228.     SCP_LIST=$SCP_LIST"spawn scp ndb_node_start.sh root@$ndb_ip:/etc/ndb_node_start.sh"$'\n'
  229.     SCP_LIST=$SCP_LIST"expect \"*password*\""$'\n'
  230.     SCP_LIST=$SCP_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  231.     SCP_LIST=$SCP_LIST"expect eof"$'\n'$'\n'
  232. done

  233. for sql_ip in $SQL_IPS;do
  234.     SCP_LIST=$SCP_LIST"spawn scp my_mysql.cnf root@$sql_ip:/etc/my.cnf"$'\n'
  235.     SCP_LIST=$SCP_LIST"expect \"*password*\""$'\n'
  236.     SCP_LIST=$SCP_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  237.     SCP_LIST=$SCP_LIST"expect eof"$'\n'$'\n'
  238.     
  239.     SCP_LIST=$SCP_LIST"spawn scp mysql_node.sh root@$sql_ip:/etc/mysql_node.sh"$'\n'
  240.     SCP_LIST=$SCP_LIST"expect \"*password*\""$'\n'
  241.     SCP_LIST=$SCP_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  242.     SCP_LIST=$SCP_LIST"expect eof"$'\n'$'\n'
  243. done

  244. cat > ./autogen_mysql/ConfigAllServer.sh << end
  245. #!/bin/sh
  246. /usr/bin/expect << endexpect
  247. set timeout 10

  248. spawn scp config.ini root@$MGM_IP:/var/lib/mysql/config.ini
  249. expect "*password*"
  250. send "$ROOTPASSWORD\r"
  251. expect eof

  252. spawn scp mgm_node.sh root@$MGM_IP:/etc/mgm_node.sh
  253. expect "*password*"
  254. send "$ROOTPASSWORD\r"
  255. expect eof

  256. $SCP_LIST
  257. endexpect
  258. end

  259. #Create RunAllServer.sh
  260. RUN_LIST=
  261. for ndp_ip in $NDB_IPS;do
  262.     RUN_LIST=$RUN_LIST"spawn ssh root@$ndp_ip"$'\n'
  263.     RUN_LIST=$RUN_LIST"expect \"*password*\""$'\n'
  264.     RUN_LIST=$RUN_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  265.     RUN_LIST=$RUN_LIST"expect \"#\""$'\n'
  266.     RUN_LIST=$RUN_LIST"send \"sh /etc/ndb_node_start.sh\r\""$'\n'
  267.     RUN_LIST=$RUN_LIST"expect eof"$'\n'
  268.     RUN_LIST=$RUN_LIST"send \"exit\r\""$'\n'$'\n'
  269. done

  270. for sql_ip in $SQL_IPS;do
  271.     RUN_LIST=$RUN_LIST"spawn ssh root@$sql_ip"$'\n'
  272.     RUN_LIST=$RUN_LIST"expect \"*password*\""$'\n'
  273.     RUN_LIST=$RUN_LIST"send \"$ROOTPASSWORD\r\""$'\n'
  274.     RUN_LIST=$RUN_LIST"expect \"#\""$'\n'
  275.     RUN_LIST=$RUN_LIST"send \"sh /etc/mysql_node.sh start\r\""$'\n'
  276.     RUN_LIST=$RUN_LIST"expect eof"$'\n'
  277.     RUN_LIST=$RUN_LIST"send \"exit\r\""$'\n'$'\n'
  278. done

  279. cat > ./autogen_mysql/RunAllServer.sh << end
  280. #!/bin/sh
  281. /usr/bin/expect << endexpect
  282. set timeout 10

  283. spawn ssh root@$MGM_IP
  284. expect "*password*"
  285. send "$ROOTPASSWORD\r"
  286. expect "#"
  287. send "sh /etc/mgm_node.sh start\r"
  288. expect eof
  289. send "exit\r"

  290. $RUN_LIST
  291. endexpect
  292. end

阅读(3406) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

cqspring2012-05-16 14:07:33

很好的文章啊~~多谢啦~~~

坏坏小丸子2012-05-12 16:42:09

很好的文章啊~~多谢啦~~~

7大爷2012-05-09 22:40:53

恩,还不错的文章~博主多多分享吧

皮娃娃哈哈2012-05-08 19:59:01

呵呵,这个可以有,网上找了其他地方都没看见~不错