├── .gitignore ├── LICENSE ├── Vagrantfile ├── README.md └── tmpfsmysql /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | *.log 3 | tmpfsmysql.cfg 4 | persist 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Martin Georgiev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # All Vagrant configuration is done below. The "2" in Vagrant.configure 5 | # configures the configuration version (we support older styles for 6 | # backwards compatibility). Please don't change it unless you know what 7 | # you're doing. 8 | Vagrant.configure(2) do |config| 9 | # The most common configuration options are documented and commented below. 10 | # For a complete reference, please see the online documentation at 11 | # https://docs.vagrantup.com. 12 | 13 | # Every Vagrant development environment requires a box. You can search for 14 | # boxes at https://atlas.hashicorp.com/search. 15 | config.vm.box = "ubuntu/zesty64" 16 | 17 | # Disable automatic box update checking. If you disable this, then 18 | # boxes will only be checked for updates when the user runs 19 | # `vagrant box outdated`. This is not recommended. 20 | # config.vm.box_check_update = false 21 | 22 | # Create a forwarded port mapping which allows access to a specific port 23 | # within the machine from a port on the host machine. In the example below, 24 | # accessing "localhost:8080" will access port 80 on the guest machine. 25 | # config.vm.network "forwarded_port", guest: 80, host: 8080 26 | 27 | # Create a private network, which allows host-only access to the machine 28 | # using a specific IP. 29 | config.vm.network "private_network", ip: "192.168.33.10" 30 | 31 | # Create a public network, which generally matched to bridged network. 32 | # Bridged networks make the machine appear as another physical device on 33 | # your network. 34 | config.vm.network "public_network" 35 | 36 | # Share an additional folder to the guest VM. The first argument is 37 | # the path on the host to the actual folder. The second argument is 38 | # the path on the guest to mount the folder. And the optional third 39 | # argument is a set of non-required options. 40 | # config.vm.synced_folder "../data", "/vagrant_data" 41 | 42 | # Provider-specific configuration so you can fine-tune various 43 | # backing providers for Vagrant. These expose provider-specific options. 44 | # Example for VirtualBox: 45 | # 46 | config.vm.provider "virtualbox" do |vb| 47 | # # Display the VirtualBox GUI when booting the machine 48 | # vb.gui = true 49 | # 50 | # # Customize the amount of memory on the VM: 51 | vb.memory = "1024" 52 | end 53 | # 54 | # View the documentation for the provider you are using for more 55 | # information on available options. 56 | 57 | # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies 58 | # such as FTP and Heroku are also available. See the documentation at 59 | # https://docs.vagrantup.com/v2/push/atlas.html for more information. 60 | # config.push.define "atlas" do |push| 61 | # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" 62 | # end 63 | 64 | # Enable provisioning with a shell script. Additional provisioners such as 65 | # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the 66 | # documentation for more information about their specific syntax and use. 67 | config.vm.provision "shell", env: {DEBIAN_FRONTEND: 'noninteractive'}, inline: <<-SHELL 68 | sudo -E apt-get update 69 | sudo -E apt-get purge -y chef puppet 70 | sudo -E apt-get install -q -y git htop mysql-server 71 | sudo -E mysqladmin -u root password root >>/dev/null 2>>/dev/null 72 | sudo -E ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ 73 | sudo -E apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld 74 | git clone https://github.com/martingeorg/tmpfs-mysql.git >>/dev/null 2>>/dev/null 75 | echo 'Cloned tmpfs-mysql in the home folder.'; 76 | echo 'You can now do "vagrant ssh" and then "cd tmpfs-mysql" and "sudo ./tmpfsmysql" to see available options.'; 77 | SHELL 78 | end 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tmpfs-mysql 2 | 3 | ### Speed up your tests using MySQL/MariaDB server with tmpfs datadir :runner::zap: 4 | 5 | ![tmpfs mysql screenshot](http://martingeorg.github.io/tmpfsmysqlscreen-06-2017.png) 6 | 7 | ### This repository has two key parts 8 | - Bash script instantiating a mysql server with datadir set to a tmpfs mounted filesystem 9 | - Vagrantfile which can be used to fire up Ubuntu instance where the script can be used 10 | 11 | 12 | #### The bash file 13 | The Bash script can be used to fire up a MySQL/MariaDB server instance whose datadir is located 14 | in a tmpfs(in memory/RAM) filesystem mounted in the /tmp/tmpfs-mysql/datadir folder. 15 | 16 | The 'normal' mysql server is kept intact. 17 | 18 | 19 | #### The Vagrantfile 20 | If you don't like the idea of having secondary mysql server instance running on your machine, there is a Vagrantfile 21 | which could be used to fire up a vagrant Ubuntu server instance. 22 | The Vagrant file has provisioning script which installs the *mysql-server* and removes *chef* and *puppet* 23 | as we won't need them and they just use memory. 24 | 25 | ### Why would i need this? 26 | Having a mysql server instance with datadir in the memory, makes executing queries very... very fast. 27 | 28 | This is usefull when running unit tests using PHPUnit, Codeception, etc. As the fixtures are being reloaded into the 29 | database on every test method, that unloading/loading of fixtures becomes slower and slower with the rising amount of fixtures. 30 | 31 | With mysql server having its datadir in memory, the fixtures loading/unloading is no longer a time waster. 32 | 33 | It is **IMPORTANT** to note that the tmpfs is **NOT A PERMANENT** storage, so don't use it for data which needs to persist. 34 | Test fixtures are disposable so we don't care about persistence. 35 | 36 | 37 | ### How to use it? 38 | 39 | ##### Compatability 40 | This script currently works with **Ubuntu** based Linux distributions and Debian. 41 | 42 | Supported MySQL server versions are 43 | - 5.5 44 | - 5.6 45 | - 5.7 46 | 47 | MariaDB is also supported. The code for MariaDB support was tested with version 10.0.27, but other versions should work as well. 48 | 49 | Future versions will add support for other distributions. 50 | 51 | ##### Before we begin 52 | The user for connecting to the tmpfs database is... wait for it... "tmpfs" :) 53 | 54 | The default password set in the PASSWORD variable is 'drowssap'. 55 | 56 | The port for the tmpfs mysql instance, set in the PORT variable is 3344. 57 | 58 | 59 | ##### In order to use the script, just follow these steps 60 | - run the script 61 | ./tmpfsmysql.sh 62 | - it'll ask for your password so it can sudo 63 | - on its first run, the script will create a configuration file "tmpfsmysql.cfg", take a look at it and adjust the configuration to your needs 64 | - to start the tmpfs-mysql server run the script again with the start option 65 | ./tmpfsmysql.sh start 66 | 67 | You can now connect to the tmpfs mysql instance using the following command (given that you haven't changed the settings in the script) 68 | 69 | mysql -u tmpfs --host=0.0.0.0 --port=3344 --password=drowssap 70 | 71 | Instead of 0.0.0.0 you can use your computer's LAN IP address, e.g. 192.168.x.x 72 | 73 | 74 | ##### If you want to use the script with the vagrant Ubuntu instance just execute the following commands from within the folder containing the Vagrantfile 75 | - vagrant up 76 | - vagrant ssh 77 | - cd /vagrant 78 | - sudo ./tmpfsmysql.sh 79 | - ifconfig 80 | - exit 81 | 82 | The *ifconfig* command is just so you can see what network address has been given to the virtual machine. 83 | 84 | You can now connect to the tmpfs mysql instance using the following command (given that you haven't changed the settings in the script) 85 | 86 | mysql -u tmpfs --host=192.168.33.10 --port=3344 --password=drowssap 87 | 88 | The IP address 192.168.33.10 is the *host-only* address of the virtual machine, you can also use the *bridged* network address (the one you saw using *ifconfig*) to access the mysql server locally or from another machine in your network. 89 | 90 | 91 | ##### What's next? 92 | Now simply configure your tests to use that mysql instance as database server and enjoy *the fastest tests alive* :runner::zap: 93 | 94 | 95 | ##### How to stop the tmpfs mysql server instance? 96 | Just issue the following command ./tmpfsmysql.sh stop 97 | 98 | 99 | ### Looking forward :construction: 100 | - [x] Add option in config file for custom parameters to the mysqld starting command. 101 | - [ ] ~~Automatic execution of the tmpfsmysql.sh should be implemented on vagrant up.~~ 102 | - [x] The 'normal' mysql server instance being restarted should be fixed. 103 | - [ ] Detect the Linux distribution and use the relevant commands 104 | - [x] Detect MySQL server version and use the specific parameters 105 | - [x] MariaDB support 106 | -------------------------------------------------------------------------------- /tmpfsmysql: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function initVars { 4 | TMPFS_FOLDER='/tmp/tmpfs-mysql' 5 | SOCKET_FILE="$TMPFS_FOLDER/tmpfs-mysqld.sock" 6 | 7 | CONFIG_FILE='tmpfsmysql.cfg' 8 | 9 | LOGFILE='./tmpfsmysql.log' 10 | LOGFILE_LINES=1000 11 | 12 | MESSAGE_INSTALLING_MYSQL="Installing the new mysql database in the tmpfs directory..." 13 | MESSAGE_STARTING_MYSQL="Starting the tmpfs mysql server with specific parameters in order to use the tmpfs datadir..." 14 | MESSAGE_SUDO_ACCESS_REQUIRED="\nThe script needs sudo access in order to work\n" 15 | } 16 | 17 | function loadConfig { 18 | if ! ( test -e "$CONFIG_FILE" -a -r "$CONFIG_FILE" -a -f "$CONFIG_FILE" ); then 19 | createConfig 20 | fi 21 | 22 | source "$CONFIG_FILE" 23 | } 24 | 25 | function createConfig { 26 | echo -e "\n\E[1;29;45mLooks like this is the first run on the script, we'll now create\E[0m" 27 | echo -e "\E[1;29;45mconfig file $CONFIG_FILE in the current folder.\E[0m" 28 | 29 | touch "$CONFIG_FILE" 30 | 31 | ( 32 | cat <<-CONFIGCONTENT 33 | # tmpfs filesystem size in MB, values below 256 are not recommended 34 | TMPFS_SIZE=256 35 | PORT=3344 36 | PASSWORD='drowssap' 37 | 38 | # additional parameters for the mysqld command 39 | # sql-mode could be used to make MySQL 5.7 behave more like MySQL 5.6 40 | # see more here http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sql_mode 41 | # --sql-mode=NO_ENGINE_SUBSTITUTION 42 | MYSQLDPARAMS='' 43 | 44 | DBNAMES[0]='' 45 | DUMPFILES[0]='' 46 | # import database, host:port:user:pass:dbname 47 | # if host, port or user are not provided, they will default to 127.0.0.1, 3306 and root respectively 48 | IMPORTSFROM[0]='::::' 49 | RUNCOMMANDS[0]='' 50 | CONFIGCONTENT 51 | ) > $CONFIG_FILE 52 | } 53 | 54 | function populatePID { 55 | PID=`sudo cat $TMPFS_FOLDER/tmpfs-mysqld.pid 2>/dev/null` 56 | } 57 | 58 | function populateServerVersionVariables { 59 | MYSQL_VERSION_RAW=`mysqld --version 2>>$LOGFILE` 60 | SERVER_MODE='MySQL' 61 | 62 | if [[ "$MYSQL_VERSION_RAW" == *"MariaDB"* ]]; then 63 | SERVER_MODE='MariaDB' 64 | fi 65 | 66 | MYSQL_VERSION_XX=`echo "$MYSQL_VERSION_RAW" | awk '{ print $3 }' | awk -F"." '{ print $1 $2 }' | awk -F"-" '{ print $1 }'` 67 | MYSQL_VERSION_XXX=`echo "$MYSQL_VERSION_RAW" | awk '{ print $3 }' | awk -F"." '{ print $1 $2 $3 }' | awk -F"-" '{ print $1 }'` 68 | MYSQL_VERSION_Xxx=`echo "$MYSQL_VERSION_RAW" | awk '{ print $3 }' | awk -F"." '{ print $1 }' | awk -F"-" '{ print $1 }'` 69 | MYSQL_VERSION_xXx=`echo "$MYSQL_VERSION_RAW" | awk '{ print $3 }' | awk -F"." '{ print $2 }' | awk -F"-" '{ print $1 }'` 70 | MYSQL_VERSION_xxX=`echo "$MYSQL_VERSION_RAW" | awk '{ print $3 }' | awk -F"." '{ print $3 }' | awk -F"-" '{ print $1 }'` 71 | } 72 | 73 | function sudoStateCheck { 74 | # Prompt user for sudo password only if needed 75 | sudo -nv 2> /dev/null 76 | if [ $? == 1 ]; then 77 | echo -e $MESSAGE_SUDO_ACCESS_REQUIRED 78 | sudo -v >>$LOGFILE 79 | fi 80 | } 81 | 82 | function checkForMySQL { 83 | local MYSQLDINSTALLED=`which mysqld 2>>$LOGFILE` 84 | if [ "$MYSQLDINSTALLED" == "" ] 85 | then 86 | echo "" 87 | echo "You don't seem to have MySQL server installed! Please install it before running this script." 88 | echo "" 89 | exit 0 90 | fi 91 | } 92 | 93 | function displayMenu { 94 | echo -ne "\n\E[1;32;40mTMPFS-MYSQL server management tool" 95 | tput sgr0 96 | 97 | echo -ne "\n\E[1;34;40mYou are using $SERVER_MODE version $MYSQL_VERSION_Xxx.$MYSQL_VERSION_xXx.$MYSQL_VERSION_xxX" 98 | tput sgr0 99 | 100 | echo -e "\n" 101 | echo -ne " \E[1;32;49mAvailable command options are:\E[0m" 102 | echo -e "\n" 103 | echo " start|restart - Starts the tmpfs MySQL instance. This will kill any already started tmpfs mysql server." 104 | echo " stop - Stop the tmpfs mysql server instance." 105 | echo " status - Check whether the tmpfs mysql server is running." 106 | echo " Script exit status will be 0 if the tmpfs mysql server is running, 1 if it's not." 107 | echo " kill - Kill any other instances of the mysqld daemon besides the normal MySQL server." 108 | echo -ne " \E[2;31;49mIt will however gracefully restart the normal MySQL server!"; tput sgr0 109 | echo -e "\n" 110 | echo " showdb - Show a list of the databases on the tmpfs mysql server." 111 | echo -ne " createdb - Create database with the given name. Example \E[1;36;44m./tmpfsmysql createdb testdb"; tput sgr0 112 | echo -e "" 113 | echo -ne " dropdb - Drop database with the given name. Example \E[1;36;44m./tmpfsmysql dropdb testdb"; tput sgr0 114 | echo -e "\n" 115 | echo " store - Persist the current data from the tmpfs-mysql server into the persist folder." 116 | echo " store-clear - Clears the data stored in the persist folder." 117 | echo " load - Load the persisted data from the persist folder into the tmpfs-mysql server data dir." 118 | echo 119 | echo " usage - Shows how much of the memory, alocated to the tmpfs storage is in use." 120 | echo " client - You will be connected to the tmpfs mysql server using the mysql client." 121 | echo " checkdump - Check if the dump file configured in DUMPFILE exists and is readable." 122 | echo -e "" 123 | exit 0 124 | } 125 | 126 | function handleMenuChoice { 127 | # call sudoStateCheck only if the requested action needs it 128 | # populatePID requires sudo, so we include the 'status' action as well 129 | if [ "$1" == "status" -o "$1" == "usage" -o "$1" == "store" -o "$1" == "load" -o "$1" == "stop" -o "$1" == "kill" -o "$1" == "start" -o "$1" == "restart" ] 130 | then 131 | sudoStateCheck 132 | fi 133 | 134 | if [ "$1" == "status" -o "$1" == "store" -o "$1" == "load" -o "$1" == "stop" -o "$1" == "kill" -o "$1" == "start" -o "$1" == "restart" ] 135 | then 136 | populatePID 137 | fi 138 | 139 | # now lets see what action has been requested 140 | if [ "$1" == "" ]; then 141 | displayMenu 142 | fi 143 | 144 | if [ "$1" == "client" ]; then 145 | menuActionConnectAsClient 146 | fi 147 | 148 | if [ "$1" == "showdb" ]; then 149 | menuActionShowDb 150 | fi 151 | 152 | if [ "$1" == "createdb" ]; then 153 | menuActionCreateDb $1 $2 154 | fi 155 | 156 | if [ "$1" == "dropdb" ]; then 157 | menuActionDropDb $1 $2 158 | fi 159 | 160 | if [ "$1" == "checkdump" ]; then 161 | menuActionCheckSQLdumpFile $1 $2 162 | fi 163 | 164 | if [ "$1" == "stop" ]; then 165 | menuActionStop 166 | fi 167 | 168 | if [ "$1" == "status" ]; then 169 | menuActionStatus 170 | fi 171 | 172 | if [ "$1" == "kill" ]; then 173 | menuActionKill 174 | fi 175 | 176 | if [ "$1" == "store" ]; then 177 | menuActionStore 178 | fi 179 | 180 | if [ "$1" == "load" ]; then 181 | menuActionLoad 182 | fi 183 | 184 | if [ "$1" == "store-clear" ]; then 185 | menuActionStoreClear 186 | fi 187 | 188 | if [ "$1" == "usage" ]; then 189 | menuActionUsage 190 | fi 191 | 192 | if [ "$1" == "start" ] || [ "$1" == "restart" ]; then 193 | menuActionStartRestart 194 | fi 195 | } 196 | 197 | function nicelyStopTmpfsServer { 198 | if [ "$PID" != "" ]; then 199 | echo "tmpfs-mysql PID is $PID" 200 | sudo kill -s 15 "$PID" >>$LOGFILE 2>>$LOGFILE 201 | echo "tmpfs-mysql server has been put to sleep" 202 | fi 203 | } 204 | 205 | function killByPID { 206 | if [ "$PID" != "" ]; then 207 | echo "Terminating tmpfs mysqld process with id $PID..." 208 | sudo kill -s 9 $PID >>$LOGFILE 2>>$LOGFILE 209 | fi 210 | } 211 | 212 | function cleanTmpfsFilesystem { 213 | echo "Delete the in memory temporary file system..." 214 | sudo umount -l $TMPFS_FOLDER >>$LOGFILE 2>>$LOGFILE 215 | sudo rm -rf $TMPFS_FOLDER >>$LOGFILE 2>>$LOGFILE 216 | } 217 | 218 | function prepareTmpfsFilesystem { 219 | cleanTmpfsFilesystem 220 | 221 | echo "Creating temporary file system in RAM..." 222 | sudo mkdir $TMPFS_FOLDER >>$LOGFILE 2>>$LOGFILE 223 | sudo mount -t tmpfs -o size="$TMPFS_SIZE"M tmpfs $TMPFS_FOLDER >>$LOGFILE 2>>$LOGFILE 224 | 225 | sudo chown mysql:mysql $TMPFS_FOLDER 226 | sudo -u mysql mkdir $TMPFS_FOLDER/datadir 227 | } 228 | 229 | function installBaseDatabaseMySQLold { 230 | sudo mysql_install_db --user=mysql --datadir=$TMPFS_FOLDER/datadir >>$LOGFILE 2>>$LOGFILE 231 | /bin/sleep 1 232 | } 233 | 234 | function installBaseDatabaseMySQLnew { 235 | sudo -u mysql mysqld --initialize-insecure --user=mysql --datadir=$TMPFS_FOLDER/datadir >>$LOGFILE 2>>$LOGFILE 236 | /bin/sleep 1 237 | } 238 | 239 | function installBaseDatabaseinitMariaDB { 240 | sudo mysql_install_db --user=mysql --datadir=$TMPFS_FOLDER/datadir >>$LOGFILE 2>>$LOGFILE 241 | /bin/sleep 1 242 | } 243 | 244 | function dbServerInstaller { 245 | echo $MESSAGE_INSTALLING_MYSQL 246 | 247 | if [ "$SERVER_MODE" == 'MariaDB' ]; then 248 | installBaseDatabaseinitMariaDB 249 | else 250 | if [ "$MYSQL_VERSION_XX" == "55" -o "$MYSQL_VERSION_XX" == "56" ]; then 251 | installBaseDatabaseMySQLold 252 | fi 253 | 254 | if [ "$MYSQL_VERSION_XX" == "57" -a "${MYSQL_VERSION_xxX}" -lt 6 ]; then 255 | installBaseDatabaseMySQLold 256 | fi 257 | 258 | if [ "$MYSQL_VERSION_Xxx" -ge "5" -a "${MYSQL_VERSION_xXx}" -ge 7 -a "${MYSQL_VERSION_xxX}" -ge 6 ]; then 259 | installBaseDatabaseMySQLnew 260 | fi 261 | fi 262 | } 263 | 264 | function dbServerStarter { 265 | echo $MESSAGE_STARTING_MYSQL 266 | sudo -u mysql mysqld --datadir=$TMPFS_FOLDER/datadir --pid-file=$TMPFS_FOLDER/tmpfs-mysqld.pid --socket=$SOCKET_FILE --port=$PORT \ 267 | --log-error=$TMPFS_FOLDER/error.log --bind-address=0.0.0.0 --innodb_flush_log_at_trx_commit=2 --performance_schema=0 $MYSQLDPARAMS >>$LOGFILE 2>>$LOGFILE & 268 | /bin/sleep 1 269 | } 270 | 271 | function fixUserAccessAndPrivileges { 272 | echo "Set default password and allow access from any host..." 273 | 274 | if [ "$SERVER_MODE" == 'MariaDB' ]; 275 | then 276 | sudo mysqladmin -u root --host=localhost --port=$PORT --socket=$SOCKET_FILE password $PASSWORD >>$LOGFILE 2>>$LOGFILE 277 | 278 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 279 | -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 280 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 281 | -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 282 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 283 | -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'0.0.0.0' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 284 | 285 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 286 | -e "CREATE USER 'tmpfs'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 287 | 288 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 289 | -e "GRANT ALL PRIVILEGES ON *.* TO 'tmpfs'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 290 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 291 | -e "GRANT ALL PRIVILEGES ON *.* TO 'tmpfs'@'localhost' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 292 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 293 | -e "GRANT ALL PRIVILEGES ON *.* TO 'tmpfs'@'0.0.0.0' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 294 | 295 | sudo mysql -u root --host=localhost --port=$PORT --password=$PASSWORD --socket=$SOCKET_FILE \ 296 | -e 'FLUSH PRIVILEGES;' >>$LOGFILE 2>>$LOGFILE 297 | else 298 | mysqladmin -u root --host=0.0.0.0 --port=$PORT password $PASSWORD >>$LOGFILE 2>>$LOGFILE 299 | mysql -u root --host=0.0.0.0 --port=$PORT --password=$PASSWORD \ 300 | -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 301 | mysql -u root --host=0.0.0.0 --port=$PORT --password=$PASSWORD \ 302 | -e "CREATE USER 'tmpfs'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 303 | mysql -u root --host=0.0.0.0 --port=$PORT --password=$PASSWORD \ 304 | -e "GRANT ALL PRIVILEGES ON *.* TO 'tmpfs'@'%' IDENTIFIED BY '$PASSWORD';" >>$LOGFILE 2>>$LOGFILE 305 | mysql -u root --host=0.0.0.0 --port=$PORT --password=$PASSWORD \ 306 | -e 'FLUSH PRIVILEGES;' >>$LOGFILE 2>>$LOGFILE 307 | fi 308 | 309 | echo 310 | } 311 | 312 | function performAfterStartUserConfigTasks { 313 | for DBINDEX in "${!DBNAMES[@]}" 314 | do 315 | if [ "${DBNAMES[$DBINDEX]}" != "" ] 316 | then 317 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD -e "create database ${DBNAMES[$DBINDEX]};" >>$LOGFILE 2>>$LOGFILE 318 | 319 | if [ "${DUMPFILES[$DBINDEX]}" != "" ] 320 | then 321 | if test -e "${DUMPFILES[$DBINDEX]}" -a -r "${DUMPFILES[$DBINDEX]}" -a -f "${DUMPFILES[$DBINDEX]}" 322 | then 323 | echo -ne '\E[1;29;42m'; 324 | echo -n "Importing sql dump file"; tput sgr0 325 | echo "" 326 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD "${DBNAMES[$DBINDEX]}" < "${DUMPFILES[$DBINDEX]}" >>$LOGFILE 2>>$LOGFILE 327 | else 328 | echo -ne '\E[1;29;41m'; 329 | echo -n "Couldn't find or read sql dump file, please check the path and try again"; tput sgr0 330 | echo "" 331 | fi 332 | fi 333 | 334 | if [ "${IMPORTSFROM[$DBINDEX]}" != "::::" -a "${IMPORTSFROM[$DBINDEX]}" != "" ]; then 335 | IMPORT_HOST=`echo "${IMPORTSFROM[$DBINDEX]}" | awk -F":" '{ print $1 }' ` 336 | IMPORT_PORT=`echo "${IMPORTSFROM[$DBINDEX]}" | awk -F":" '{ print $2 }' ` 337 | IMPORT_USER=`echo "${IMPORTSFROM[$DBINDEX]}" | awk -F":" '{ print $3 }' ` 338 | IMPORT_PASS=`echo "${IMPORTSFROM[$DBINDEX]}" | awk -F":" '{ print $4 }' ` 339 | IMPORT_NAME=`echo "${IMPORTSFROM[$DBINDEX]}" | awk -F":" '{ print $5 }' ` 340 | 341 | if [ "$IMPORT_HOST" == "" ]; then IMPORT_HOST="127.0.0.1"; fi 342 | if [ "$IMPORT_PORT" == "" ]; then IMPORT_PORT="3306"; fi 343 | if [ "$IMPORT_USER" == "" ]; then IMPORT_USER="root"; fi 344 | 345 | if [ "$IMPORT_PASS" != "" -a "$IMPORT_NAME" != "" ]; then 346 | echo -ne "\E[1;33;45mImporting $IMPORT_NAME database from $IMPORT_HOST"; tput sgr0; echo -ne "\n" 347 | mysqldump -u "$IMPORT_USER" --password="$IMPORT_PASS" --host="$IMPORT_HOST" --port="$IMPORT_PORT" "$IMPORT_NAME" > "./export.sql" 2>>$LOGFILE 348 | if test -e "./export.sql" -a -r "./export.sql" -a -f "./export.sql"; then 349 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD "${DBNAMES[$DBINDEX]}" < "./export.sql" >>$LOGFILE 2>>$LOGFILE 350 | fi 351 | rm -f "./export.sql" >>$LOGFILE 2>>$LOGFILE 352 | fi 353 | 354 | fi 355 | 356 | if [ "${RUNCOMMANDS[$DBINDEX]}" != "" ] 357 | then 358 | echo -ne "\E[1;29;40mExecuting the command configured for database $DBINDEX..."; tput sgr0; echo 359 | ${RUNCOMMANDS[$DBINDEX]} >>$LOGFILE 2>>$LOGFILE 360 | fi 361 | 362 | fi 363 | done 364 | } 365 | 366 | function checkStartSuccess { 367 | local PIDONSTART=`sudo cat $TMPFS_FOLDER/tmpfs-mysqld.pid 2>/dev/null` 368 | if [ "$PIDONSTART" != "" ] 369 | then 370 | echo -ne "\E[1;29;42mThe tmpfs mysql server has started"; tput sgr0; echo -ne '\n' 371 | echo "The password for the tmpfs-mysql server is '$PASSWORD' and the port is '$PORT'." 372 | else 373 | echo -ne "\E[1;29;41mThe tmpfs mysql server failed to start "; tput sgr0; echo -ne '\n' 374 | fi 375 | echo -ne "\n" 376 | } 377 | 378 | function rotateLogFile { 379 | if test -e "$LOGFILE" -a -r "$LOGFILE" -a -f "$LOGFILE" 380 | then 381 | logfileLines=`wc -l $LOGFILE | awk '{print $1}'` 382 | 383 | if [ "${logfileLines}" -gt 1000 ]; then 384 | cp "$LOGFILE" "$LOGFILE".bak 385 | tail -n $LOGFILE_LINES "$LOGFILE".bak > "$LOGFILE" 386 | rm -f "$LOGFILE".bak 387 | fi 388 | fi 389 | } 390 | 391 | function menuActionCheckSQLdumpFile { 392 | if [ "$1" == "" ]; then 393 | echo -ne "\E[1;33;43mNo database index from the config was specified, assuming 0..."; tput sgr0; echo -ne "\n" 394 | local CHKDUMPINDEX=0 395 | else 396 | local CHKDUMPINDEX=$1 397 | fi 398 | 399 | if test -e "${DUMPFILES[$CHKDUMPINDEX]}" -a -r "${DUMPFILES[$CHKDUMPINDEX]}" -a -f "${DUMPFILES[$CHKDUMPINDEX]}" 400 | then 401 | echo -ne '\E[1;29;42m'; 402 | echo -n "The SQL dump file at '${DUMPFILES[$CHKDUMPINDEX]}' exists and is readable." 403 | tput sgr0 404 | echo "" 405 | else 406 | echo -ne '\E[1;29;41m'; 407 | echo -n "The SQL dump file at '${DUMPFILES[$CHKDUMPINDEX]}' either does not exists or is not readable." 408 | tput sgr0 409 | echo "" 410 | fi 411 | 412 | echo "" 413 | exit 0 414 | } 415 | 416 | function menuActionConnectAsClient { 417 | echo -ne '\n\E[3;29;44m'"Logging into the tmpfs mysql server..."; tput sgr0 418 | echo "" 419 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD 2>>$LOGFILE 420 | exit 0 421 | } 422 | 423 | function menuActionShowDb { 424 | echo -ne "\n\E[3;29;44mShowing databases on the tmpfs mysql server..."; tput sgr0 425 | echo "" 426 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD -e 'show databases' 2>>$LOGFILE 427 | echo "" 428 | exit 0 429 | } 430 | 431 | function menuActionCreateDb { 432 | echo -ne "\n\E[3;29;44mCreating database $2 on the tmpfs mysql server..."; tput sgr0 433 | echo "" 434 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD -e "create database if not exists $2" 2>>$LOGFILE 435 | echo "" 436 | exit 0 437 | } 438 | 439 | function menuActionDropDb { 440 | echo -ne "\n\E[3;29;44mDropping database $2 from the tmpfs mysql server..."; tput sgr0 441 | echo "" 442 | mysql -u tmpfs --host=0.0.0.0 --port=$PORT --password=$PASSWORD -e "drop database if exists $2" 2>>$LOGFILE 443 | echo "" 444 | exit 0 445 | } 446 | 447 | function menuActionStore { 448 | 449 | if ! ( test -e $TMPFS_FOLDER/datadir -a -r $TMPFS_FOLDER/datadir -a -d $TMPFS_FOLDER/datadir ) 450 | then 451 | echo -e "\n\E[3;31;40mThe tmpfs-mysql server doesn't seem to have been started, we cannot copy from it's datadir. Exiting with status 1.\E[0m\n" 452 | exit 1 453 | fi 454 | 455 | echo -e "Storing the tmpfs-mysql server datadir into the persist folder..." 456 | 457 | nicelyStopTmpfsServer 458 | 459 | sleep 4 460 | 461 | persistFolder=`pwd`/persist 462 | persistFolderDatadir=`pwd`/persist/datadir 463 | 464 | echo -e "We are about to delete the current data in the $persistFolderDatadir folder"; 465 | 466 | sudo rm -rf "$persistFolderDatadir" 467 | mkdir -p "$persistFolder" 468 | sudo cp -pr $TMPFS_FOLDER/datadir "$persistFolderDatadir" 469 | 470 | dbServerStarter 471 | echo -e "...completed persisting the tmpfs-mysql datadir." 472 | } 473 | 474 | function menuActionLoad { 475 | # need to handle cases where load is called before tmpfs-mysql is started and we need to create the tmpfs folder 476 | 477 | if ! ( test -e $TMPFS_FOLDER/datadir -a -r $TMPFS_FOLDER/datadir -a -d $TMPFS_FOLDER/datadir ) 478 | then 479 | echo -e "\n\E[3;31;40mThe tmpfs-mysql server doesn't seem to have been started, we cannot copy to it's datadir. Exiting with status 1.\E[0m\n" 480 | exit 1 481 | fi 482 | 483 | persistFolderDatadir=`pwd`/persist/datadir 484 | 485 | if ! ( test -e "$persistFolderDatadir" -a -r "$persistFolderDatadir" -a -d "$persistFolderDatadir" ) 486 | then 487 | echo -e "\nNo datadir is present in the persist folder, exiting with status 1!\n" 488 | exit 1 489 | fi 490 | 491 | echo -e "Loading the tmpfs-mysql server datadir from the persist folder..." 492 | 493 | nicelyStopTmpfsServer 494 | 495 | echo -e "We are about to load the $persistFolderDatadir folder as tmpfs-mysql datadir"; 496 | 497 | sudo rm -rf $TMPFS_FOLDER/datadir 498 | # figure out a better way than sleep, to wait for rm to complete and have the filesystem stats updated 499 | sleep 2 500 | sudo cp -pr "$persistFolderDatadir" $TMPFS_FOLDER/datadir 501 | 502 | dbServerStarter 503 | 504 | echo -e "...completed loading tmpfs-mysql datadir from the persist folder." 505 | } 506 | 507 | function menuActionStoreClear { 508 | echo -e "Clearing the persist folder..." 509 | 510 | persistFolderDatadir=`pwd`/persist/datadir 511 | echo -e "We are about to delete the $persistFolderDatadir folder"; 512 | 513 | sudo rm -rf "$persistFolderDatadir" 514 | 515 | echo -e "...completed clearing the persist folder." 516 | } 517 | 518 | function menuActionUsage { 519 | if ! ( test -e $TMPFS_FOLDER/datadir -a -r $TMPFS_FOLDER/datadir -a -d $TMPFS_FOLDER/datadir ) 520 | then 521 | echo -e "The tmpfs-mysql server doesn't seem to have been started, we cannot fetch stats.\nExiting with status 1." 522 | exit 1 523 | fi 524 | 525 | usedMB=`sudo du -sm $TMPFS_FOLDER | awk '{ print $1 }'`; echo -e "\n\E[1;39;46m Memory used \E[0m\E[1;39;44m ${usedMB} of ${TMPFS_SIZE} MB \E[0m\n" 526 | } 527 | 528 | function menuActionStartRestart { 529 | killByPID 530 | prepareTmpfsFilesystem 531 | dbServerInstaller 532 | dbServerStarter 533 | 534 | echo "Waiting for the new mysql server instance to fire up before we continue..." 535 | /bin/sleep 1 536 | 537 | fixUserAccessAndPrivileges 538 | performAfterStartUserConfigTasks 539 | checkStartSuccess 540 | } 541 | 542 | function menuActionStop { 543 | nicelyStopTmpfsServer 544 | echo "The tmpfs mysql server instance has been stopped." 545 | cleanTmpfsFilesystem 546 | echo -e "Done" 547 | exit 0 548 | } 549 | 550 | function menuActionStatus { 551 | if [ "$PID" != "" ] 552 | then 553 | echo -ne "\nThe tmpfs mysql server seems to be "; echo -ne '\E[1;29;42m'" running "; tput sgr0 554 | echo -e "\n" 555 | exit 0 556 | else 557 | echo -ne "\nThe tmpfs mysql server seems to be "; echo -ne '\E[1;29;41m'" down "; tput sgr0 558 | echo -e "\n" 559 | exit 1 560 | fi 561 | } 562 | 563 | function menuActionKill { 564 | killByPID 565 | MYSQLSERVICEPID=`sudo cat /var/run/mysqld/mysqld.pid 2>/dev/null` 566 | 567 | echo -e "You gave the kill order, we'll now stop the normal mysql server if it is running\n and kill any other instances of the mysqld daemon..." 568 | 569 | if [ "$MYSQLSERVICEPID" != "" ] 570 | then 571 | echo "Gracefully stopping the normal mysql server and killing any other mysqld process..." 572 | sudo service mysql stop >>$LOGFILE 2>>$LOGFILE 573 | /bin/sleep 1 574 | fi 575 | 576 | sudo killall mysqld >>$LOGFILE 2>>$LOGFILE 577 | /bin/sleep 1 578 | cleanTmpfsFilesystem 579 | 580 | if [ "$MYSQLSERVICEPID" != "" ] 581 | then 582 | echo "Starting up the normal mysql server..." 583 | sudo service mysql restart >>$LOGFILE 2>>$LOGFILE #& 584 | fi 585 | exit 0 586 | } 587 | 588 | initVars 589 | checkForMySQL 590 | populateServerVersionVariables 591 | loadConfig 592 | handleMenuChoice $1 $2 593 | rotateLogFile 594 | --------------------------------------------------------------------------------