| Installing and configuring mySQL | ||||||||||
| This article describes how I installed mySQL, a very popular
relational database, especially as a back-end database for web servers. I won't be teaching you how to use SQL. Sorry, but that is way beyond the scope of this website yet alone this article. |
||||||||||
| Resources I have found | ||||||||||
These are the resources I used when installing mySQL.
|
||||||||||
| The background | ||||||||||
| I'm surprised it's taken me so long to get around to using mySQL.
I've been involved in client server database applications for almost 15 years.
mySQL should have been one of my first ventures. When I decided to add the forum to the website, the process was rather straight forward. My web site provider already supplied mySQL and php3 support, but I wanted that at home as well. |
||||||||||
| Installing | ||||||||||
Remember, I have all the ports installed. So it was
easy. I originally tried mySQL321, but that failed to build/install.
I can't recall why.cd /usr/ports/databases/mysql322 make make install |
||||||||||
| Getting it running | ||||||||||
The easiest way to get mysql running is to use the installed script:# /usr/local/etc/rc.d/mysql.sh You should now see something this:
|
||||||||||
| Making it more secure | ||||||||||
I created a user to run the mysql daemon. This is deemed to be more
secure than running a daemon as root. If the daemon is compromised, then it doesn't
have root privileges. Here's the entry from vipw. Your numbers may
be different, but the basics are the same. I suggest you use adduser to do
this.mysql:*:1010:1010::0:0:mysql daemon:/nonexistent:/sbin/nologin Then I modifed the /usr/local/etc/rc.d/mysql.sh startup script to include the --user parameter. Here is what my file looks like now. The bit I added is in bold. #!/bin/sh # /sbin/ldconfig -m /usr/local/lib/mysql if [ -x /usr/local/bin/safe_mysqld ] then /usr/local/bin/safe_mysqld --user=mysql > /dev/null & && echo -n ' mysql' fi In addition to the above, you'll also need to change the file permissions on the databases. See the next section for more information. |
||||||||||
| The database files | ||||||||||
In conjunction with the user change in the previous section, you should also change
the permissions on the database files. I used the opportunity to move the database
files to another location. By default, the database files are located in
/var/db/mysql. This can be changed by modifying the script /usr/local/bin/safe_mysqld.
But I prefer to leave scripts unchanged and just move the files. So I did
this:cd /var/db mv mysql /usr/local/ ln -s /usr/local/mysql mysql Then I changed the permissions. cd /usr/local chown -R mysql mysql And then restarted mysqld. /usr/local/etc/rc.d/mysql.sh |
||||||||||
| The sysadmin password | ||||||||||
This bit is what I originally wrote regarding the sysadmin password:
Anthony Rubin wrote in with better information than I originally supplied. I thank him for that.
Yes Andrew, that's does help out. Thank you. |
||||||||||
| Shutdown | ||||||||||
After setting the sysadmin password, here is how you shutdown mysqld:# mysqladmin --password shutdown Enter password: |
||||||||||
| Creating a database | ||||||||||
http://www.devshed.com/Server_Side/Administration/Database/page6.html
contains details on how to create a database. But here's what I did. It is
important to note that this user root is not the UNIX user root. It is the
mySQL user. Remember to supply the password you specified when you set the sysadmin
password.# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 3.22.22 Type 'help' for help. mysql> create database firstone; Query OK, 1 row affected (0.05 sec) You now have a database. If you check under /var/db/mysql, you'll see a new directory for firstone. This is the database. |
||||||||||
| Creating mySQL users | ||||||||||
| In this stage, we create database users. These are not UNIX users with a login.
These are logical users. When you connect to the database, you must supply a
user id and password. I would suggest not using the same logins for both UNIX and mySQL,
just in case. Similarly, don't use the same password for mySQL and UNIX.
That's just asking for trouble. See http://www.devshed.com/Server_Side/Administration/Database/page6.html for details on how to create a new mySQL user. Here is how I created a user, testuser, and gave them permissions on everything in my database called test. mysql> grant usage on test.* to testuser@localhost; Query OK, 0 rows affected (0.02 sec) mysql> grant select, insert,delete on test.* to testuser@localhost; Query OK, 0 rows affected (0.02 sec) You can now login to the database as testuser and retrieve the data. Note that I first created the table mytable as shown at http://www.devshed.com/Server_Side/Administration/Database/page6.html. # mysql -u testuser Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 to server version: 3.22.22 Type 'help' for help. mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from mytable; +----------------+----------+ | name | phone | +----------------+----------+ | Homer Simpson | 555-1234 | | Bart Simpson | 555-4321 | | Lisa Simpson | 555-3214 | | Marge Simpson | 555-2314 | | Maggie Simpson | 555-3142 | +----------------+----------+ 5 rows in set (0.02 sec) |
||||||||||
| Backups | ||||||||||
Here is the script I use for backing up my database. This file is
also available from database_dump.sh.txt which
is in a better format for copy/paste.
|
||||||||||
| That should get you started | ||||||||||
| I hope that all worked for you. If it didn't, please let me know and I'll update the article. | ||||||||||
|