|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Installing MySQL |
| 4 | +--- |
| 5 | + |
| 6 | +Installing MySQL |
| 7 | +================ |
| 8 | + |
| 9 | +<div markdown="1" class="tutorial" data-author="Whitespace" data-facets='{"Operating System": "Ubuntu", "Package Management": "APT", "MySQL Version": "5.1"}'> |
| 10 | +To install MySQL, run the following command from a terminal prompt: |
| 11 | + |
| 12 | +{% highlight sh %} |
| 13 | +sudo apt-get install mysql-server |
| 14 | +{% endhighlight %} |
| 15 | + |
| 16 | +During the installation process you will be prompted to enter a password for the __MySQL__ root user. |
| 17 | + |
| 18 | +Once the installation is complete, the MySQL server should be started automatically. You can run the following command from a terminal prompt to check whether the MySQL server is running: |
| 19 | + |
| 20 | +{% highlight sh %} |
| 21 | +sudo netstat -tap | grep mysql |
| 22 | +{% endhighlight %} |
| 23 | + |
| 24 | +When you run this command, you should see the following line or something similar: |
| 25 | + |
| 26 | +{% highlight sh %} |
| 27 | +tcp 0 0 localhost.localdomain:mysql *:* LISTEN - |
| 28 | +{% endhighlight %} |
| 29 | + |
| 30 | +If the server is not running correctly, you can type the following command to start it: |
| 31 | + |
| 32 | +{% highlight sh %} |
| 33 | +sudo /etc/init.d/mysql restart |
| 34 | +{% endhighlight %} |
| 35 | + |
| 36 | +Configuration |
| 37 | +------------- |
| 38 | +You can edit the `/etc/mysql/my.cnf` file to configure the basic settings -- log file, port number, etc. For example, to configure __MySQL__ to listen for connections from network hosts, change the `bind_address` directive to the server's IP address: |
| 39 | + |
| 40 | +{% highlight sh %} |
| 41 | +bind-address = 192.168.0.5 |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +Note Replace 192.168.0.5 with the appropriate address. |
| 45 | + |
| 46 | +After making a change to `/etc/mysql/my.cnf` the __mysql__ daemon will need to be restarted: |
| 47 | + |
| 48 | +{% highlight sh %} |
| 49 | +sudo /etc/init.d/mysql restart |
| 50 | +{% endhighlight %} |
| 51 | +</div> |
| 52 | + |
| 53 | +<div markdown="1" class="tutorial" data-author="Whitespace" data-facets='{"Operating System": "OS X", "Package Management": "Source", "MySQL Version": "5.1"}'> |
| 54 | +These are instructions for compiling and installing a 64-bit version of [MySQL](http://www.mysql.com/), the world's most popular open source database, on Mac OS X 10.6 (Snow Leopard). |
| 55 | + |
| 56 | +The benefits of manually building MySQL yourself in `/usr/local` are detailed [here](http://hivelogic.com/articles/using_usr_local/). I also wrote a in-depth explanation of why you might want to build MySQL yourself in my [Compiling MySQL on Leopard](http://hivelogic.com/articles/installing-mysql-on-mac-os-x/) article. |
| 57 | + |
| 58 | +NO SUPPORT |
| 59 | +---------- |
| 60 | +These instructions will probably work just fine and you won't run into any trouble, but if you do, **please don't email me about it**. I wish I could help everybody who might have an issue, but I just don't have the time to help you troubleshoot hundreds of potential variables with your specific configuration, and I probably won't even reply to the email, and I'm sorry in advance for that. |
| 61 | + |
| 62 | +PREREQUISITES |
| 63 | +------------- |
| 64 | +Before following these instructions, you will need: |
| 65 | + |
| 66 | +- Mac OS X 10.6 Snow Leopard |
| 67 | +- The latest Xcode Tools (from the Snow Leopard DVD or downloaded from [Apple](http://developer.apple.com/) - the 10.5 version _won't work_) |
| 68 | +- Confidence running UNIX commands using the Terminal |
| 69 | + |
| 70 | +If you want to learn more about UNIX and the command line, check out [my PeepCode screencast](http://peepcode.com/products/meet-the-command-line) on this topic. |
| 71 | + |
| 72 | +STEP 1: SET THE PATH |
| 73 | +-------------------- |
| 74 | +Launch Terminal.app from the `/Applications/Utilities` folder. |
| 75 | + |
| 76 | +We need to set your shell's `PATH` variable first. The `PATH` variable determines where your system searches for command-line programs. Using the editor of your choice, create and edit a file in your home directory named `.profile` (note the "." preceding the filename). |
| 77 | + |
| 78 | +If you're using [TextMate](http://macromates.com/) like you should be and have [installed the UNIX `mate` command](http://manual.macromates.com/en/using_textmate_from_terminal.html), then you can create and start editing the file like this: |
| 79 | + |
| 80 | +{% highlight sh %} |
| 81 | +mate ~/.profile |
| 82 | +{% endhighlight %} |
| 83 | + |
| 84 | +To the end of this file, add the following line (or verify that it's already there): |
| 85 | + |
| 86 | +{% highlight sh %} |
| 87 | +export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" |
| 88 | +{% endhighlight %} |
| 89 | + |
| 90 | +Close and save the file and run this command to load the new setting into your current shell: |
| 91 | + |
| 92 | +source ~/.profile |
| 93 | +To verify that you've updated your path, enter the following command: |
| 94 | + |
| 95 | +{% highlight sh %} |
| 96 | +echo $PATH |
| 97 | +{% endhighlight %} |
| 98 | + |
| 99 | +You should see `/usr/local/bin` at the beginning of the line returned by the system. |
| 100 | + |
| 101 | +STEP 2: DOWNLOAD |
| 102 | +---------------- |
| 103 | + |
| 104 | +We're going to create a folder to contain the files we're about to download and compile. If you want, you can delete this folder when you're done, but keeping it around makes it easier to re-install (or uninstall) these apps later. |
| 105 | + |
| 106 | +Make the new folder: |
| 107 | + |
| 108 | +{% highlight sh %} |
| 109 | +mkdir ~/src |
| 110 | +cd ~/src |
| 111 | +{% endhighlight %} |
| 112 | + |
| 113 | +I used to provide a link to download the latest version of MySQL, but they update frequently and sometimes the links die, so instead, please go to the MySQL site, and download the latest version from [this page](http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.39.tar.gz/from/pick#mirrors), picking a mirror near you. Then move or copy it to your `src` folder. |
| 114 | + |
| 115 | +For those of you who expect the world, you can try this command, but please don't email me if it's broken: |
| 116 | + |
| 117 | +{% highlight sh %} |
| 118 | +curl -O http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.39.tar.gz |
| 119 | +{% endhighlight %} |
| 120 | + |
| 121 | +STEP 3: COMPILE AND INSTALL |
| 122 | +--------------------------- |
| 123 | + |
| 124 | +Build and install MySQL like this: |
| 125 | + |
| 126 | +{% highlight sh %} |
| 127 | +tar xzvf mysql-5.1.37.tar.gz |
| 128 | +cd mysql-5.1.37 |
| 129 | +./configure --prefix=/usr/local/mysql --with-extra-charsets=complex --enable-thread-safe-client --enable-local-infile --enable-shared --with-plugins=innobase |
| 130 | +make |
| 131 | +sudo make install |
| 132 | +cd /usr/local/mysql |
| 133 | +sudo ./bin/mysql_install_db --user=mysql |
| 134 | +sudo chown -R mysql ./var |
| 135 | +cd .. |
| 136 | +{% endhighlight %} |
| 137 | + |
| 138 | +STARTING (AND AUTO-STARTING) MYSQL |
| 139 | +---------------------------------- |
| 140 | + |
| 141 | +In most cases, you'll want MySQL to auto-start every time you boot (or reboot) your Mac. The easiest way to do this is using launchd, Mac OS X's infrastructure for managing system processes. |
| 142 | + |
| 143 | +I've prepared a launchd plist file that will allow you to manage MySQL, starting it at boot and stopping it cleanly at shutdown. Save the plist file to your `~/src` directory and then move it to the proper place with the following commands: |
| 144 | + |
| 145 | +{% highlight sh %} |
| 146 | +cd ~/src |
| 147 | +curl -O http://hivelogic.com/downloads/com.mysql.mysqld.plist |
| 148 | +sudo mv ~/src/com.mysql.mysqld.plist /Library/LaunchDaemons |
| 149 | +sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist |
| 150 | +{% endhighlight %} |
| 151 | + |
| 152 | +Finally, tell `launchd` to load and startup MySQL: |
| 153 | + |
| 154 | +{% highlight sh %} |
| 155 | +sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist |
| 156 | +{% endhighlight %} |
| 157 | + |
| 158 | +If you see no response, it probably means that everything worked correctly, and that MySQL is running. You can verify this by launching MySQL's command-line monitor: |
| 159 | + |
| 160 | +{% highlight sh %} |
| 161 | +mysql -uroot |
| 162 | +{% endhighlight %} |
| 163 | + |
| 164 | +You should see something like this: |
| 165 | + |
| 166 | +{% highlight sh %} |
| 167 | +Welcome to the MySQL monitor. Commands end with ; or g. |
| 168 | +Your MySQL connection id is 1 |
| 169 | +Server version: 5.1.37 Source distribution |
| 170 | +Type 'help;' or 'h' for help. Type 'c' to clear the buffer. |
| 171 | +mysql> |
| 172 | +{% endhighlight %} |
| 173 | + |
| 174 | +If this prompt appears, MySQL has been installed correctly. Type `exit` and press return to quit the MySQL monitor. |
| 175 | + |
| 176 | +If you didn't see that message, it's possible that something "bad" happened, or that you may have missed a step above. You can always try running through the instructions again. |
| 177 | + |
| 178 | +You now have a custom-built 64-bit version of MySQL. |
| 179 | + |
| 180 | +STARTING AND STOPPING MYSQL MANUALLY |
| 181 | +------------------------------------ |
| 182 | + |
| 183 | +If you ever want to stop MySQL manually, use this command: |
| 184 | + |
| 185 | +{% highlight sh %} |
| 186 | +sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist |
| 187 | +{% endhighlight %} |
| 188 | + |
| 189 | +To (re)start MySQL manually, use this command: |
| 190 | + |
| 191 | +{% highlight sh %} |
| 192 | +sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist |
| 193 | +{% endhighlight %} |
| 194 | + |
| 195 | +EXTRA CREDIT: THE RAILS GEM |
| 196 | +--------------------------- |
| 197 | + |
| 198 | +You can install the Rails MySQL Gem by pointing it at your new MySQL installation direcory: |
| 199 | + |
| 200 | +{% highlight sh %} |
| 201 | +sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql |
| 202 | +{% endhighlight %} |
| 203 | + |
| 204 | +That should do it. |
| 205 | +</div> |
| 206 | + |
| 207 | +<div markdown="1" class="tutorial" data-facets='{"Operating System": "OS X", "Package Management": "Homebrew", "MySQL Version": "5.5"}'> |
| 208 | +If you already have a `/usr/local` folder and it's not owned by your user: |
| 209 | + |
| 210 | +{% highlight sh %} |
| 211 | +sudo chown -R `whoami` /usr/local |
| 212 | +{% endhighlight %} |
| 213 | + |
| 214 | +Install Homebrew: |
| 215 | + |
| 216 | +{% highlight sh %} |
| 217 | +cd /usr/local |
| 218 | +git init |
| 219 | +git remote add origin git://github.com/mxcl/homebrew.git |
| 220 | +git pull origin master |
| 221 | +{% endhighlight %} |
| 222 | + |
| 223 | +This is kind of odd -- you install Homebrew right into the base of your `/usr/local` folder. It nicely ignores other folders that already exists there. Just do it. |
| 224 | + |
| 225 | +Install MySQL: |
| 226 | + |
| 227 | +{% highlight sh %} |
| 228 | +brew install mysql |
| 229 | +{% endhighlight %} |
| 230 | + |
| 231 | +Yeah, it's really that easy. This will take a while. |
| 232 | + |
| 233 | +Now warm it up: |
| 234 | + |
| 235 | +{% highlight sh %} |
| 236 | +mysql_install_db |
| 237 | +{% endhighlight %} |
| 238 | + |
| 239 | +And make sure it automatically starts again on login: |
| 240 | + |
| 241 | +{% highlight sh %} |
| 242 | +launchctl load -w /usr/local/Cellar/mysql/5.1.43/com.mysql.mysqld.plist |
| 243 | +{% endhighlight %} |
0 commit comments