blob: e2c83433d23db66c41f550b9c03570d09bb4e490 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyer6d04fd72012-12-21 11:03:37 -06003# lib/databases/mysql
4# Functions to control the configuration and operation of the **MySQL** database backend
Terry Wilson428af5a2012-11-01 16:12:39 -04005
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - DATABASE_{HOST,USER,PASSWORD} must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04009
10# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110011_XTRACE_DB_MYSQL=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040012set +o xtrace
13
armando-migliacciob3d88222015-06-12 07:54:03 -070014MYSQL_DRIVER=${MYSQL_DRIVER:-PyMySQL}
Dean Troyercc6b4432013-04-08 15:38:03 -050015
Terry Wilson428af5a2012-11-01 16:12:39 -040016register_database mysql
17
Sean Dague53753292014-12-04 19:38:15 -050018# Linux distros, thank you for being incredibly consistent
19MYSQL=mysql
Wiekus Beukesec47bc12015-03-19 08:20:38 -070020if is_fedora && ! is_oraclelinux; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010021 MYSQL=mariadb
Sean Dague53753292014-12-04 19:38:15 -050022fi
Dean Troyercc6b4432013-04-08 15:38:03 -050023
24# Functions
25# ---------
26
Julien Danjou0eec4f82015-09-08 10:45:06 +000027function get_database_type_mysql {
28 if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
29 echo mysql+pymysql
30 else
31 echo mysql
32 fi
33}
34
Dean Troyer995eb922013-03-07 16:11:40 -060035# Get rid of everything enough to cleanly change database backends
36function cleanup_database_mysql {
Sean Dague53753292014-12-04 19:38:15 -050037 stop_service $MYSQL
Dean Troyer995eb922013-03-07 16:11:40 -060038 if is_ubuntu; then
39 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050040 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060041 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030042 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060043 return
Wiekus Beukesec47bc12015-03-19 08:20:38 -070044 elif is_suse || is_oraclelinux; then
45 uninstall_package mysql-community-server
46 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060047 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010048 uninstall_package mariadb-server
49 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060050 else
51 return
52 fi
Dean Troyer995eb922013-03-07 16:11:40 -060053}
54
Terry Wilson428af5a2012-11-01 16:12:39 -040055function recreate_database_mysql {
56 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080057 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020058 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040059}
60
61function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050062 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040063 echo_summary "Configuring and starting MySQL"
64
Vincent Untzc18b9652012-12-04 12:36:34 +010065 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050066 my_conf=/etc/mysql/my.cnf
67 mysql=mysql
Wiekus Beukesec47bc12015-03-19 08:20:38 -070068 elif is_suse || is_oraclelinux; then
69 my_conf=/etc/my.cnf
70 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010071 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010072 mysql=mariadb
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010074 else
75 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040076 fi
77
78 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010079 if is_fedora || is_suse; then
80 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050081 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010082 fi
83
84 # Set the root password - only works the first time. For Ubuntu, we already
Jens Rosenboom9abb26d2016-12-07 21:12:55 +010085 # did that with debconf before installing the package, but we still try,
86 # because the package might have been installed already.
87 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
Vincent Untz00011c02012-12-06 09:56:32 +010088
Marian Horbanea21eb42015-08-18 06:57:18 -040089 # Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
Terry Wilson428af5a2012-11-01 16:12:39 -040090 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
91
92 # Now update ``my.cnf`` for some local needs and restart the mysql service
93
Marian Horbanea21eb42015-08-18 06:57:18 -040094 # Change bind-address from localhost (127.0.0.1) to any (::) and
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020095 # set default db type to InnoDB
Ian Wienand9c0b9f32015-07-22 06:08:09 +100096 iniset -sudo $my_conf mysqld bind-address "$SERVICE_LISTEN_ADDRESS"
Roman Podoliaka88b84092017-02-07 13:34:12 +020097 iniset -sudo $my_conf mysqld sql_mode TRADITIONAL
Ian Wienand9c0b9f32015-07-22 06:08:09 +100098 iniset -sudo $my_conf mysqld default-storage-engine InnoDB
Amrith Kumar1e663882017-02-27 13:29:03 -050099
100 # the number of connections has been throttled to 256. In the
101 # event that the gate jobs report "Too many connections" it is
102 # indicative of a problem that could be the result of one of many
103 # things. For more details about debugging this error, refer
104 # https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html.
105 # Note that the problem may not ONLY be an issue with MySQL
106 # connections. If the number of fd's at the OS is too low, you
107 # could see errors manifest as MySQL "too many connections".
108 iniset -sudo $my_conf mysqld max_connections 256
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000109 iniset -sudo $my_conf mysqld query_cache_type OFF
110 iniset -sudo $my_conf mysqld query_cache_size 0
Terry Wilson428af5a2012-11-01 16:12:39 -0400111
Amrith Kumar1e663882017-02-27 13:29:03 -0500112 # Additional settings to put MySQL on a memory diet. These
113 # settings are used in conjunction with the cap on max_connections
114 # as the total memory used by MySQL can be simply viewed as
115 # fixed-allocations + max_connections * variable-allocations. A
116 # nifty tool to help with this is
117 # http://www.mysqlcalculator.com/. A short description of each of
118 # the settings follows.
119
120 # binlog_cache_size, determines the size of cache to hold changes
121 # to the binary log during a transaction, for each connection. For
122 # more details, refer
123 # https://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.html#sysvar_binlog_cache_size
124 # When binary logging is enabled, a smaller binlog cache could
125 # result in more frequent flushes to the disk and a larger value
126 # would result in less flushes to the disk but higher memory
127 # usage. This however only has to do with large transactions; if
128 # you have a small transaction the binlog cache is necessarily
129 # flushed on a transaction commit. This is a per-connection cache.
130 iniset -sudo $my_conf mysqld binlog_cache_size 4K
131
132 # binlog_stmt_cache_size determines the size of cache to hold non
133 # transactional statements in the binary log. For more details,
134 # refer
135 # https://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.html#sysvar_binlog_stmt_cache_size
136 # This cache holds changes to non-transactional tables (read:
137 # MyISAM) or any non-transactional statements which cause
138 # modifications to data (truncate is an example). These are
139 # written to disk immediately on completion of the statement or
140 # when the cache is full. If the cache is too small, you get
141 # frequent writes to the disk (flush) and if the cache is too
142 # large, it takes up more memory. This is a per-connection cache.
143 iniset -sudo $my_conf mysqld binlog_stmt_cache_size 4K
144
145 # bulk_insert_buffer_size for MyISAM tables that use a special
146 # cache for insert statements and load statements, this cache is
147 # used to optimize writes to the disk. If the value is set to 0,
148 # the optimization is disabled. For more details refer
149 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bulk_insert_buffer_size
150 # We set this to 0 which could result in higher disk I/O (I/O on
151 # each insert block completion).
152 iniset -sudo $my_conf mysqld bulk_insert_buffer_size 0
153
154 # host_cache_size controls a DNS lookup optimization. For more
155 # details refer
156 # https://dev.mysql.com/doc/refman/5.6/en/host-cache.html
157 iniset -sudo $my_conf mysqld host_cache_size 0
158
159 # innodb_buffer_pool_size This is the size of the server wide
160 # buffer pool. It is the cache for all data blocks being used by
161 # the server and is managed as a LRU chain. Dirty blocks either
162 # age off the list or are forced off when the list is
163 # full. Setting this to 5MB (default 128MB) reduces the amount of
164 # memory used by the server and this will result in more disk I/O
165 # in cases where (a) there is considerable write activity that
166 # overwhelms the allocated cache, or (b) there is considerable
167 # read activity on a data set that exceeds the allocated
168 # cache. For more details, refer
169 # https://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_buffer_pool_size
170 iniset -sudo $my_conf mysqld innodb_buffer_pool_size 5M
171
172 # innodb_ft_cache_size and innodb_ft_total_cache_size control the
173 # per-connection full text search cache and the server wide
174 # maximum full text search cache. We should not be using full text
175 # search and the value is set to the minimum allowable. The former
176 # is a per-connection cache size and the latter is server
177 # wide. For more details, refer
178 # https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_ft_cache_size
179 # and
180 # https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_ft_total_cache_size
181 iniset -sudo $my_conf mysqld innodb_ft_cache_size 1600000
182 iniset -sudo $my_conf mysqld innodb_ft_total_cache_size 32000000
183
184 # innodb_log_buffer_size This buffer is used to buffer
185 # transactions in-memory before writing them to the innodb
186 # internal transaction log. Large transactions, or high amounts of
187 # concurrency, will cause the system to fill this faster and thus
188 # make the system more disk-bound. For more details, refer
189 # https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_log_buffer_size
190 iniset -sudo $my_conf mysqld innodb_log_buffer_size 256K
191
192 # innodb_sort_buffer_size, This buffer is used for sorting when
193 # InnoDB is creating indexes. Could cause that to be slower, but
194 # only if tables are large. This is a per-connection setting. For
195 # more details, refer
196 # https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_sort_buffer_size
197 iniset -sudo $my_conf mysqld innodb_sort_buffer_size 64K
198
199 # join_buffer_size, This buffer makes table and index scans
200 # faster. So this setting could make some queries more disk
201 # bound. This is a per-connection setting. For more details refer
202 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_join_buffer_size.
203 iniset -sudo $my_conf mysqld join_buffer_size 128
204
205 # key_buffer_size defines the index blocks used for MyISAM tables
206 # and shared between threads. This is a server wide setting. For
207 # more details see
208 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_key_buffer_size
209 iniset -sudo $my_conf mysqld key_buffer_size 8
210
211 # max_heap_table_size sets the maximum amount of memory for MEMORY
212 # tables (which we don't use). The value is set to 16k, the
213 # minimum allowed. For more details, see
214 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_heap_table_size
215 iniset -sudo $my_conf mysqld max_heap_table_size 16K
216
217 # net_buffer_length Each client has a buffer for incoming and
218 # outgoing data, both start with a size of net_buffer_length and
219 # can grow (in steps of 2x) upto a size of max_allowed_packet. For
220 # more details see
221 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_net_buffer_length
222 iniset -sudo $my_conf mysqld net_buffer_length 1K
223
224 # read_buffer_size, read_rnd_buffer_size are per-thread buffer
225 # used for scans on MyISAM tables. It is a per-connection setting
226 # and so we set it to the minimum value allowable. Same for
227 # read_rnd_buffer_size. For more details refer
228 # https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_read_buffer_size
229 # and
230 # https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_read_rnd_buffer_size
231 iniset -sudo $my_conf mysqld read_buffer_size 8200
232 iniset -sudo $my_conf mysqld read_rnd_buffer_size 8200
233
234 # sort_buffer_size when a sort is requested, it will be performed
235 # in memory in a buffer of this size (allocated per connection)
236 # and if the data exceeds this size it will spill to disk. The
237 # innodb and myisam variables are used in computing indices for
238 # tables using the specified storage engine. Since we don't
239 # dynamically reindex (except during upgrade) these values should
240 # never be material. Obviously performance of disk based sorts is
241 # worse than in memory sorts and therefore a high value here will
242 # improve sort performance for large data. For more details,
243 # refer:
244 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sort_buffer_size
245 # and
246 # https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_sort_buffer_size
247 # and
248 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_myisam_sort_buffer_size
249 iniset -sudo $my_conf mysqld sort_buffer_size 32K
250 iniset -sudo $my_conf mysqld innodb_sort_buffer_size 64K
251 iniset -sudo $my_conf mysqld myisam_sort_buffer_size 4K
252
253 # thread_cache_size specifies how many internal threads to cache
254 # for use with incoming connections. We set this to 0 whic means
255 # that each connection will cause a new thread to be created. This
256 # could cause connections to take marginally longer on os'es with
257 # slow pthread_create calls. For more details, refer
258 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_thread_cache_size
259 iniset -sudo $my_conf mysqld thread_cache_size 0
260
261 # thread_stack is the per connection stack size, the minimum is
262 # 128k and the default is 192k on 32bit and 256k on 64bit
263 # systems. We set this to 192k. Complex queries which require
264 # recursion, stored procedures or other memory intensive
265 # operations could exhaust this and generate a very characteristic
266 # failure ("stack overflow") which is cleanly detected and the
267 # query is killed. For more details see
268 # https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_thread_stack
269 iniset -sudo $my_conf mysqld thread_stack 196608
270
271 # tmp_table_size is the maximum size of an in-memory temporary
272 # table. Temporary tables are created by MySQL as part of a
273 # multi-step query plan. The actual size of the temp table will be
274 # the lesser of tmp_table_size and max_heap_table_size. If a
275 # temporary table exceeds this size, it will be spooled to disk
276 # using the internal_tmp_disk_storage_engine (default
277 # MyISAM). Queries that often generate in-memory temporary tables
278 # include queries that have sorts, distinct, or group by
279 # operations, also queries that perform IN joins. For more details
280 # see
281 # https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_tmp_table_size
282 iniset -sudo $my_conf mysqld tmp_table_size 1K
283
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000284 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
285 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +0100286 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200287 slow_log=/var/log/mariadb/mariadb-slow.log
288 else
289 slow_log=/var/log/mysql/mysql-slow.log
290 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200291 sudo sed -e '/log.slow.queries/d' \
292 -e '/long.query.time/d' \
293 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500294 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000295
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200296 # Turn on slow query log, log all queries (any query taking longer than
297 # 0 seconds) and log all non-indexed queries
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000298 iniset -sudo $my_conf mysqld slow-query-log 1
299 iniset -sudo $my_conf mysqld slow-query-log-file $slow_log
300 iniset -sudo $my_conf mysqld long-query-time 0
301 iniset -sudo $my_conf mysqld log-queries-not-using-indexes 1
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000302 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000303
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500304 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400305}
306
307function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100308 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400309 # Seed configuration with mysql password so that apt-get install doesn't
310 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000311 sudo debconf-set-selections <<MYSQL_PRESEED
312mysql-server mysql-server/root_password password $DATABASE_PASSWORD
313mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
314mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400315MYSQL_PRESEED
316 fi
317
318 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
319 # as it allows you to access the mysql databases via ``mysql nova`` instead
320 # of having to specify the username/password each time.
321 if [[ ! -e $HOME/.my.cnf ]]; then
322 cat <<EOF >$HOME/.my.cnf
323[client]
324user=$DATABASE_USER
325password=$DATABASE_PASSWORD
Johan Pas199d8572015-11-17 00:56:25 +0100326host=$MYSQL_HOST
Terry Wilson428af5a2012-11-01 16:12:39 -0400327EOF
328 chmod 0600 $HOME/.my.cnf
329 fi
330 # Install mysql-server
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700331 if is_suse || is_oraclelinux; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200332 if ! is_package_installed mariadb; then
333 install_package mysql-community-server
334 fi
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700335 elif is_fedora; then
336 install_package mariadb-server
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -0400337 sudo systemctl enable mariadb
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700338 elif is_ubuntu; then
339 install_package mysql-server
Vincent Untzca5c4712012-11-21 17:45:49 +0100340 else
Vincent Untz00011c02012-12-06 09:56:32 +0100341 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100342 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500343}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600344
Dean Troyer5686dbc2015-03-09 14:27:51 -0500345function install_database_python_mysql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600346 # Install Python client module
Sean Dague37421992015-05-20 06:37:11 -0700347 pip_install_gr $MYSQL_DRIVER
348 if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
349 ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
Julien Danjou0f63eb32015-06-12 09:05:12 +0200350 elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
351 ADDITIONAL_VENV_PACKAGES+=",PyMySQL"
Sean Dague37421992015-05-20 06:37:11 -0700352 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400353}
354
355function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100356 local db=$1
357 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400358}
359
Dean Troyercc6b4432013-04-08 15:38:03 -0500360
Terry Wilson428af5a2012-11-01 16:12:39 -0400361# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100362$_XTRACE_DB_MYSQL
Sean Dague584d90e2013-03-29 14:34:53 -0400363
364# Local variables:
365# mode: shell-script
366# End: