Merge lp:~clint-fewbar/charms/oneiric/mysql/add-config into lp:charms/oneiric/mysql

Proposed by Clint Byrum
Status: Merged
Merged at revision: 69
Proposed branch: lp:~clint-fewbar/charms/oneiric/mysql/add-config
Merge into: lp:charms/oneiric/mysql
Diff against target: 416 lines (+299/-38)
9 files modified
config.yaml (+25/-0)
hooks/config-changed (+233/-0)
hooks/install (+7/-19)
hooks/master-relation-changed (+4/-3)
hooks/slave-relation-broken (+9/-0)
hooks/slave-relation-changed (+16/-13)
hooks/slave-relation-departed (+0/-2)
hooks/upgrade-charm (+4/-0)
revision (+1/-1)
To merge this branch: bzr merge lp:~clint-fewbar/charms/oneiric/mysql/add-config
Reviewer Review Type Date Requested Status
Juan L. Negron (community) Approve
Review via email: mp+84697@code.launchpad.net

Description of the change

This branch adds some high level config parameters and commonly desirable direct tunables to the mysql charm. All changes to config cause mysql to restart.

It also removes the use of augeas for mysql (its still used for apache configs). So the charm now fully generates the whole mysql config itself.

To post a comment you must log in.
Revision history for this message
Juan L. Negron (negronjl) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'config.yaml'
--- config.yaml 1970-01-01 00:00:00 +0000
+++ config.yaml 2011-12-07 00:11:24 +0000
@@ -0,0 +1,25 @@
1options:
2 dataset-size:
3 default: 1G
4 description: How much data do you want to keep in memory in the DB. This will be used to tune settings in the database server appropriately. Any more specific settings will override these defaults though. This currently sets innodb_buffer_pool_size or key_cache_size depending on the setting in preferred-storage-engine. If query-cache-type is set to 'ON' or 'DEMAND' 10% of this is given to query-cache-size.
5 type: string
6 preferred-storage-engine:
7 default: InnoDB
8 type: string
9 description: Tune the server for usage of this storage engine. Other possible value is MyISAM. Comma separated will cause settings to split resources evenly among given engines.
10 tuning-level:
11 default: safest
12 type: string
13 description: Valid values are 'safest', 'fast', and 'unsafe'. If set to safest, all settings are tuned to have maximum safety at the cost of performance. Fast will turn off most controls, but may lose data on crashes. unsafe will turn off all protections.
14 query-cache-type:
15 default: "ON"
16 type: string
17 description: Query cache is usually a good idea, but can hurt concurrency. Valid values are "OFF", "ON", or "DEMAND". http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_query_cache_type
18 query-cache-size:
19 default: -1
20 type: int
21 description: Override the computed version from dataset-size. Still works if query-cache-type is "OFF" since sessions can override the cache type setting on their own.
22 max-connections:
23 default: -1
24 type: int
25 description: Maximum connections to allow. -1 means use the server's compiled in default.
026
=== added file 'hooks/config-changed'
--- hooks/config-changed 1970-01-01 00:00:00 +0000
+++ hooks/config-changed 2011-12-07 00:11:24 +0000
@@ -0,0 +1,233 @@
1#!/usr/bin/python
2
3from subprocess import check_output,check_call
4import tempfile
5import json
6import re
7import hashlib
8import os
9
10num_re = re.compile('^[0-9]+$')
11
12configs=json.loads(check_output(['config-get','--format=json']))
13
14# There should be a library for this
15def human_to_bytes(human):
16 if num_re.match(human):
17 return human
18 factors = { 'K' : 1024 , 'M' : 1048576, 'G' : 1073741824, 'T' : 1099511627776 }
19 modifier=human[-1]
20 if modifier in factors:
21 return int(human[:-1]) * factors[modifier]
22 raise Exception("Can only convert K,M,G, or T")
23
24# smart-calc stuff in the configs
25dataset_bytes = human_to_bytes(configs['dataset-size'])
26
27if configs['query-cache-size'] == -1 and configs['query-cache-type'] in ['ON','DEMAND']:
28 configs['query-cache-size'] = int(dataset_bytes * 0.20)
29 dataset_bytes -= configs['query-cache-size']
30
31# 5.5 allows the words, but not 5.1
32if configs['query-cache-type'] == 'ON':
33 configs['query-cache-type']=1
34elif configs['query-cache-type'] == 'DEMAND':
35 configs['query-cache-type']=2
36else:
37 configs['query-cache-type']=0
38
39preferred_engines=configs['preferred-storage-engine'].split(',')
40chunk_size = int(dataset_bytes / len(preferred_engines))
41configs['innodb-flush-log-at-trx-commit']=1
42configs['sync-binlog']=1
43if 'InnoDB' in preferred_engines:
44 configs['innodb-buffer-pool-size'] = chunk_size
45 if configs['tuning-level'] == 'fast':
46 configs['innodb-flush-log-at-trx-commit']=2
47else:
48 configs['innodb-buffer-pool-size'] = 0
49
50configs['default-storage-engine'] = preferred_engines[0]
51
52if 'MyISAM' in preferred_engines:
53 configs['key-buffer'] = chunk_size
54else:
55 # Need a bit for auto lookups always
56 configs['key-buffer'] = human_to_bytes('8M')
57
58if configs['tuning-level'] == 'fast':
59 configs['sync-binlog']=0
60
61if configs['max-connections'] == -1:
62 configs['max-connections'] = '# max_connections = ?'
63else:
64 configs['max-connections'] = 'max_connections = %s' % configs['max-connections']
65
66template="""
67######################################
68#
69#
70#
71# This file generated by the juju MySQL charm!
72#
73# Local changes will not be preserved!
74#
75#
76#
77######################################
78#
79# The MySQL database server configuration file.
80#
81# You can copy this to one of:
82# - "/etc/mysql/my.cnf" to set global options,
83# - "~/.my.cnf" to set user-specific options.
84#
85# One can use all long options that the program supports.
86# Run program with --help to get a list of available options and with
87# --print-defaults to see which it would actually understand and use.
88#
89# For explanations see
90# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
91
92# This will be passed to all mysql clients
93# It has been reported that passwords should be enclosed with ticks/quotes
94# escpecially if they contain "#" chars...
95# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
96[client]
97port = 3306
98socket = /var/run/mysqld/mysqld.sock
99
100# Here is entries for some specific programs
101# The following values assume you have at least 32M ram
102
103# This was formally known as [safe_mysqld]. Both versions are currently parsed.
104[mysqld_safe]
105socket = /var/run/mysqld/mysqld.sock
106nice = 0
107
108[mysqld]
109#
110# * Basic Settings
111#
112
113#
114# * IMPORTANT
115# If you make changes to these settings and your system uses apparmor, you may
116# also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
117#
118
119user = mysql
120socket = /var/run/mysqld/mysqld.sock
121port = 3306
122basedir = /usr
123datadir = /var/lib/mysql
124tmpdir = /tmp
125skip-external-locking
126#
127# Instead of skip-networking the default is now to listen only on
128# localhost which is more compatible and is not less secure.
129bind-address = 0.0.0.0
130#
131# * Fine Tuning
132#
133key_buffer = %(key-buffer)s
134max_allowed_packet = 16M
135# This replaces the startup script and checks MyISAM tables if needed
136# the first time they are touched
137myisam-recover = BACKUP
138%(max-connections)s
139#table_cache = 64
140#thread_concurrency = 10
141#
142# * Query Cache Configuration
143#
144query_cache_limit = 1M
145query_cache_size = %(query-cache-size)s
146query_cache_type = %(query-cache-type)s
147#
148# * Logging and Replication
149#
150# Both location gets rotated by the cronjob.
151# Be aware that this log type is a performance killer.
152# As of 5.1 you can enable the log at runtime!
153#general_log_file = /var/log/mysql/mysql.log
154#general_log = 1
155
156log_error = /var/log/mysql/error.log
157
158# Here you can see queries with especially long duration
159#log_slow_queries = /var/log/mysql/mysql-slow.log
160#long_query_time = 2
161#log-queries-not-using-indexes
162#
163# The following can be used as easy to replay backup logs or for replication.
164# note: if you are setting up a replication slave, see README.Debian about
165# other settings you may need to change.
166#server-id = 1
167#log_bin = /var/log/mysql/mysql-bin.log
168expire_logs_days = 10
169max_binlog_size = 100M
170#binlog_do_db = include_database_name
171#binlog_ignore_db = include_database_name
172#
173# * InnoDB
174#
175# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
176# Read the manual for more InnoDB related options. There are many!
177#
178innodb_buffer_pool_size = %(innodb-buffer-pool-size)s
179innodb_flush_log_at_trx_commit = %(innodb-flush-log-at-trx-commit)s
180sync_binlog = %(sync-binlog)s
181default_storage_engine = %(default-storage-engine)s
182skip-name-resolve
183# * Security Features
184#
185# Read the manual, too, if you want chroot!
186# chroot = /var/lib/mysql/
187#
188# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
189#
190# ssl-ca=/etc/mysql/cacert.pem
191# ssl-cert=/etc/mysql/server-cert.pem
192# ssl-key=/etc/mysql/server-key.pem
193
194
195
196[mysqldump]
197quick
198quote-names
199max_allowed_packet = 16M
200
201[mysql]
202#no-auto-rehash # faster start of mysql but no tab completition
203
204[isamchk]
205key_buffer = 16M
206
207#
208# * IMPORTANT: Additional settings that can override those from this file!
209# The files must end with '.cnf', otherwise they'll be ignored.
210#
211!includedir /etc/mysql/conf.d/
212"""
213
214mycnf=template % configs
215with tempfile.NamedTemporaryFile(mode='w',dir='/etc/mysql',delete=False) as t:
216 t.write(mycnf)
217 t.flush()
218 tmd5 = hashlib.md5()
219 tmd5.update(mycnf)
220 with open('/etc/mysql/my.cnf','r') as old:
221 md5=hashlib.md5()
222 md5.update(old.read())
223 oldhash = md5.digest()
224 if oldhash != tmd5.digest():
225 os.rename('/etc/mysql/my.cnf','/etc/mysql/my.cnf.%s' % md5.hexdigest())
226 os.rename(t.name, '/etc/mysql/my.cnf')
227 try:
228 check_call(['service','mysql','stop'])
229 except subprocess.CalledProcessError:
230 pass
231 check_call(['service','mysql','start'])
232 else:
233 check_call(['juju-log','my.cnf not changed, skipping restart/replace'])
0234
=== modified file 'hooks/install'
--- hooks/install 2011-09-16 21:54:23 +0000
+++ hooks/install 2011-12-07 00:11:24 +0000
@@ -15,26 +15,14 @@
1515
16unit_id=`echo $JUJU_UNIT_NAME | cut -d/ -f2`16unit_id=`echo $JUJU_UNIT_NAME | cut -d/ -f2`
1717
18# augeas 0.8.0 and later knows about my.cnf format .. otherwise use ours18# On slaves, this gets overwritten
19augvers=`dpkg -l augeas-lenses|awk '/^ii/ {print $3}'`19if [ ! -f /var/lib/juju/i.am.a.slave ] ; then
20if dpkg --compare-versions $augvers '<' 0.8.0 || \20 cat > /etc/mysql/conf.d/binlog.cnf <<EOF
21 ! [ -f /usr/share/augeas/lenses/dist/mysql.aug ] ; then21[mysqld]
22 echo augeas $augvers needs mysql.aug and inifile.aug file from formula...22server_id = $unit_id
23 mv -f /usr/share/augeas/lenses/dist/inifile.aug /var/backups23log_bin = /var/log/mysql/mysql-bin.log
24 cp -v `dirname $0`/../inifile.aug /usr/share/augeas/lenses/dist
25 cp -v `dirname $0`/../mysql.aug /usr/share/augeas/lenses/dist
26else
27 echo using mysql.aug from $augvers
28fi
29augtool <<EOF
30defnode mysqld '/files/etc/mysql/my.cnf/target[. = "mysqld"]'
31set \$mysqld/server_id $unit_id
32set \$mysqld/log_bin /var/log/mysql/mysql-bin.log
33set \$mysqld/default_storage_engine InnoDB
34set \$mysqld/bind-address 0.0.0.0
35set \$mysqld/skip-name-resolve ""
36save
37EOF24EOF
25fi
3826
39service mysql stop || :27service mysql stop || :
40service mysql start28service mysql start
4129
=== modified file 'hooks/master-relation-changed'
--- hooks/master-relation-changed 2011-11-09 19:43:42 +0000
+++ hooks/master-relation-changed 2011-12-07 00:11:24 +0000
@@ -34,7 +34,7 @@
34chown -v -R www-data.www-data /var/www34chown -v -R www-data.www-data /var/www
3535
36remote_host=`relation-get hostname`36remote_host=`relation-get hostname`
37test -n "$remote_host" || exit 137test -n "$remote_host" || exit 0
38remote_ip=`dig +short $remote_host`38remote_ip=`dig +short $remote_host`
39pass=`pwgen -s 16`39pass=`pwgen -s 16`
40augtool -b <<EOF40augtool -b <<EOF
@@ -45,9 +45,10 @@
45save45save
46EOF46EOF
47service apache2 reload47service apache2 reload
48mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$JUJU_REMOTE_UNIT\`@\`$remote_ip\` IDENTIFIED BY '$pass'"48user=`echo $JUJU_REMOTE_UNIT | sed -e 's,/,-,'`
49mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
49relation-set dumpurl=/snaps/$name \50relation-set dumpurl=/snaps/$name \
50 user=$JUJU_REMOTE_UNIT \51 user=$user \
51 password=$pass \52 password=$pass \
52 hostname=`unit-get private-address` \53 hostname=`unit-get private-address` \
53 port=330654 port=3306
5455
=== added file 'hooks/slave-relation-broken'
--- hooks/slave-relation-broken 1970-01-01 00:00:00 +0000
+++ hooks/slave-relation-broken 2011-12-07 00:11:24 +0000
@@ -0,0 +1,9 @@
1#!/bin/sh
2
3# Kill the replication
4mysql -uroot -p`cat /var/lib/juju/mysql.passwd` -e 'STOP SLAVE;'
5mysql -uroot -p`cat /var/lib/juju/mysql.passwd` -e 'RESET SLAVE;'
6# No longer a slave
7# XXX this may change the server-id .. not sure if thats what we
8# want!
9rm /var/lib/juju/i.am.a.slave
010
=== modified file 'hooks/slave-relation-changed'
--- hooks/slave-relation-changed 2011-09-16 21:54:23 +0000
+++ hooks/slave-relation-changed 2011-12-07 00:11:24 +0000
@@ -60,25 +60,28 @@
60basedir=/usr60basedir=/usr
61EOF61EOF
6262
63serverid=`augtool get /files/etc/mysql/my.cnf/target[3]/server_id | awk -F" = " '{print $2}'`
64server_id_name="server_id"
65if [ -z "$serverid" ] ; then
66 serverid=`augtool get /files/etc/mysql/my.cnf/target[3]/server-id | awk -F" = " '{print $2}'`
67 server_id_name="server-id"
68fi
69if [ -z "$serverid" ] ; then63if [ -z "$serverid" ] ; then
70 serverid=`echo $user | cut -d/ -f2`64 serverid=`echo $user | cut -d/ -f2`
71fi65fi
72if [ -z "$serverid" -o "$serverid" -lt 100000 ] ; then66# add 100000 to server_id to avoid conflicts w/ masters
73 # add 100000 to server_id to avoid conflicts w/ masters67serverid=$(($serverid+100000))
74 serverid=$(($serverid+100000))68if [ -f /etc/mysql/conf.d/slave.cf ] ; then
75 echo "set /files/etc/mysql/my.cnf/target[3]/$server_id_name $serverid\nsave" | augtool -b69 old_hash=`md5sum /etc/mysql/conf.d/slave.cf`
70else
71 old_hash="xxx"
72fi
73cat > /etc/mysql/conf.d/binlog.cnf <<EOF
74[mysqld]
75server-id = $serverid
76log_bin = /var/log/mysql/mysql-bin.log
77EOF
78new_hash=`md5sum /etc/mysql/conf.d/binlog.cnf`
79if [ "$new_hash" != "$old_hash" ] ; then
76 service mysql stop80 service mysql stop
77 # clear binlogs81 # clear any binlogs
78 binlog=`augtool get /files/etc/mysql/my.cnf/target[3]/log_bin|cut -d' ' -f3`
79 backupdir=/var/backups/binlogs-`date +%Y%m%d%H%M%S`82 backupdir=/var/backups/binlogs-`date +%Y%m%d%H%M%S`
80 mkdir -p $backupdir83 mkdir -p $backupdir
81 mv $binlog* $backupdir || :84 mv /var/log/mysql/mysql-bin* $backupdir || :
82 service mysql start85 service mysql start
83fi86fi
84mysql $ROOTARGS -e "START SLAVE"87mysql $ROOTARGS -e "START SLAVE"
8588
=== added symlink 'hooks/slave-relation-departed'
=== target is u'slave-relation-broken'
=== removed file 'hooks/slave-relation-departed'
--- hooks/slave-relation-departed 2011-09-16 21:54:23 +0000
+++ hooks/slave-relation-departed 1970-01-01 00:00:00 +0000
@@ -1,2 +0,0 @@
1#!/bin/sh
2rm /var/lib/juju/i.am.a.slave
30
=== added file 'hooks/upgrade-charm'
--- hooks/upgrade-charm 1970-01-01 00:00:00 +0000
+++ hooks/upgrade-charm 2011-12-07 00:11:24 +0000
@@ -0,0 +1,4 @@
1#!/bin/sh
2home=`dirname $0`
3$home/install
4exec $home/config-changed
05
=== modified file 'revision'
--- revision 2011-12-05 20:17:48 +0000
+++ revision 2011-12-07 00:11:24 +0000
@@ -1,1 +1,1 @@
11091118

Subscribers

People subscribed via source and target branches

to all changes: