Apache Web-Serving With Mac OS X, Part 5
Pages: 1, 2, 3
Hello, MySQL!
If the above went smoothly, it's time to make a quick PHP script to make sure database communication is possible. Copy the following code into your favorite text editor (like BBEdit), and save the file as test.php within a Web site directory (either /Library/WebServer/Documents/ or /Users/morbus/Sites, for example).
<?
print "<pre>";
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "" );
// select the 'test' database created during installation.
mysql_select_db( "test" ) or die ( mysql_error() . "\n" );
print "Connection to the database has been established.\n";
// create a simplistic table.
$table = "CREATE table wisdom (
id int(4) PRIMARY KEY AUTO_INCREMENT,
wisdom char(255), author char(125) );";
$response = mysql_query( $table, $dbh );
if ($response) { print "The table was created correctly!\n"; }
else { print mysql_error () . "\n"; }
// now, we'll add some data to our newly created table.
// to add different wisdom, just change the 'values'.
$insert_data = "INSERT into wisdom ( wisdom, author )
values ( 'Must... remain... awake!', 'Morbus' );";
$response = mysql_query( $insert_data, $dbh );
if ($response) { print "The data was inserted correctly!\n"; }
else { print mysql_error () . "\n"; }
// and read it back for printing purposes.
$get_table_data = "SELECT * FROM wisdom;";
$response = mysql_query( $get_table_data, $dbh );
if ($response) { print "We successfully got all the table data.\n"; }
else { print mysql_error () . "\n"; }
// now print it out for the user.
while ( $one_line_of_data = mysql_fetch_array( $response ) ) {
extract ( $one_line_of_data );
print "#$id: $author sez: \"$wisdom\"\n";
}
print "</pre>";
?>
Note: Again, we're not going to explore the syntax of the PHP script, or the SQL commands that are used. Suffice it to say that this script will create a table in the MySQL 'test' database, add some data, and then spit back the total contents of the 'wisdom' table. If you need a brush-up on PHP or MySQL, be sure to check out ONLamp.com.
After you've saved the file, load it in your Web browser. I saved my copy in /Users/morbus/Sites/test.php, so I loaded http://127.0.0.1/~morbus/test.php in my browser. After the first run, this is what I saw:
Connection to the database has been established.
The table was created correctly!
The data was inserted correctly!
We successfully got all the table data.
#1: Morbus sez: "Must... remain... awake!"
If I continue running the script, changing the INSERT line each time, my output will start to look like:
Connection to the database has been established.
Table 'wisdom' already exists
The data was inserted correctly!
We successfully got all the table data.
#1: Morbus sez: "Must... remain... awake!"
#2: Morbus sez: "Sleeping makes Derrick angry!"
#3: Morbus sez: "And I'm 23 minutes away from 3 o'clock!"
The above output certifies that our PHP-to-MySQL communication is working perfectly. With 23 minutes to spare before the boss and his goons come, we've got just enough time to chow down the last remnants of our ketchup potato chips and wash them down with a swig of Moxie.
Two minor additions
|
Previously in the Series Apache Web-Serving with Mac OS X, Part 6 Apache Web-Serving with Mac OS X, Part 4 Apache Web-Serving with Mac OS X: Part 3 |
When we turn on our Web server (through the Sharing preference panel), OS X will happily restart Apache if our machine ever needs a reboot. Out of the box, MySQL doesn't restart automatically. Thankfully, there's a double-clickable solution, again from Marc Liyanage. Upon installing this StartupItem, MySQL will be at your beck and call after every reboot.
With the above instructions, MySQL is woefully unsecured. Anyone can become the administrative MySQL user and wreak havoc with our data. This may not be an issue if you're using MySQL on a development machine, but publicly accessible servers need protection. Much like OS X has a root user with ultimate control over the machine, MySQL also has a root user that has ultimate control over the database server.
By default, the MySQL root user has no password assigned to it. If you take a gander back at our PHP script, you'll see that we connect to our database with that field blank:
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "" );
The simplest step in beginning to secure our database server is to set a password for MySQL's root user. To do so, enter the following in a Terminal:
mysqladmin -u root password new_password_here
Once we do that, we'll have to modify our PHP code as well:
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "new_password_here" );
This is just the start of securing a MySQL installation. You can go much deeper, like restricting access to certain databases by host name, much like we restricted access to certain Web directories with Apache's Allow and Deny directives (see "Choosing Who Sees What" in Part 3 of this series).
After the demonstration . . .
Thankfully, the meeting only lasted half an hour. The wide-eyed wonder boys have left and the redheaded stepchildren have wandered off. You're sitting, once again, with a smug look on your face, complemented by the realization that worrying about job security is a thing of the past. (Just be careful of netslavery.)
You've got databases stored, features adored, and requests explored [1]. Yet you realize there's so much more to Apache lurking beneath the surface, yelling out, "Discover me!" with a glee normally unfounded in typical Mac software. Where would you like to be taken next?
[1] Along with the bosses abhorred, benefits scored, and welcome awards, mouths floored and derision ignored, nothing deplored and happiness poured, computer love has been restored, and with no principles discord! Hail the mighty Fnord! (Yes, I could go on.)
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
Return to the Mac DevCenter.
-
Just to add
2006-08-19 19:52:09 JeremyGee [View]
-
mysql socket problem
2006-06-22 07:51:14 carlco2 [View]
-
mysql socket problem - solved differently
2009-08-23 07:27:49 ekklisis [View]
-
mysql socket problem solved.. OSX10.4
2006-06-23 04:59:13 carlco2 [View]
-
Several questions about mysql with OSX 10.3.9
2006-01-30 10:21:40 musicvideo [View]
-
Start and stop MySQL in witch program?
2005-12-09 01:16:30 NewMacUser [View]
-
MySQL root user password explaination
2004-10-16 23:40:42 joe@collabornet.org [View]
-
mysqld ended
2004-07-26 19:38:00 mel_david [View]
-
mySQL install file permissions
2004-07-17 11:53:56 cactusman [View]
-
tcsh modification...
2004-05-27 22:15:34 Durer [View]
-
tcsh modification...
2004-06-26 14:21:50 eepalmer [View]
-
Easy MySQL Install
2003-11-05 17:24:33 anonymous2 [View]
-
RE: mysql admin
2003-09-15 14:14:06 anonymous2 [View]
-
RE: mysql admin
2003-09-15 19:06:35 anonymous2 [View]
-
password?
2003-08-06 16:20:53 forgotmyscript [View]
-
Success ... then Disaster
2003-07-28 15:28:35 anonymous2 [View]
-
Success ... then Disaster
2004-12-28 06:15:10 Ginger [View]
-
Success ... then Disaster
2003-08-06 13:24:29 anonymous2 [View]
-
osX 10.1
2003-07-22 05:00:17 anonymous2 [View]
-
weird stuff!!!
2003-05-14 06:41:59 anonymous2 [View]
-
weird stuff!!!
2003-06-13 06:52:36 anonymous2 [View]
-
weird stuff!!!
2005-01-09 08:13:12 frustrated [View]
-
To deal with the password issue..
2003-05-05 15:54:00 macbuff [View]
-
1sg
2003-03-26 17:24:28 anonymous2 [View]
-
1
2003-03-26 17:24:21 anonymous2 [View]
-
Having problems with Sockets? Read THIS to fix
2003-03-26 10:10:43 anonymous2 [View]
-
mySQL up BUT...
2003-02-06 11:15:43 anonymous2 [View]
-
mod_auth_mysql
2003-01-09 16:48:26 anonymous2 [View]
-
Nice series of articles. Thanks!
2002-12-21 08:49:14 anonymous2 [View]
-
change MySQL password in 10.2 ??
2002-12-01 11:36:32 anonymous2 [View]
-
change MySQL password in 10.2 ??
2003-11-14 16:19:23 anonymous2 [View]
-
password problem - just do the following...
2002-12-10 18:20:37 tronje [View]
-
didn't work for me...
2002-12-23 16:38:00 csteinfield [View]
-
no users within phpmyadmn
2002-12-01 04:12:45 anonymous2 [View]
-
Cannot Connect to mysql.sock
2002-11-20 20:10:39 anonymous2 [View]
-
Cannot Connect to mysql.sock
2003-04-22 17:53:50 anonymous2 [View]
-
Need to uninstall MySQL
2002-11-14 03:22:21 nrussell23 [View]
-
Need to uninstall MySQL
2003-04-20 02:03:48 anonymous2 [View]
-
Access denied when creating a new DB
2002-11-08 16:01:38 anonymous2 [View]
-
PHP required a trick
2002-10-16 10:51:03 anonymous2 [View]
-
Write update of articles for Mac OS 10.2
2002-10-10 13:28:10 anonymous2 [View]
-
chown -R mysql /usr/local/mysql
2002-10-09 13:35:29 anonymous2 [View]
-
Re: chown -R mysql /usr/local/mysql
2003-11-06 00:48:19 anonymous2 [View]
-
if using 10.2 ...
2002-09-30 20:15:29 anonymous2 [View]
-
MySQL User
2002-09-29 00:33:25 anonymous2 [View]
-
MySQL User
2002-11-15 06:03:58 jumpingjokes [View]
-
need sudo to run mysqladmin?
2002-09-18 07:05:54 anonymous2 [View]
-
make (link) unresolved symbols: _tgetflag, etc. in client/
2002-09-12 11:53:41 anonymous2 [View]
-
mysqld ended...waaahhh
2002-08-30 18:45:33 elroysf [View]
-
mysqld ended...waaahhh
2003-12-15 06:01:03 anonymous2 [View]
-
mysqld ended...waaahhh
2005-06-18 00:46:07 trial_and_error [View]
-
mysqld ended...waaahhh
2003-06-13 12:57:35 anonymous2 [View]
-
Install Error That Won't Go Away!! Please Help!!
2002-08-21 19:20:52 hembeck [View]
-
Install Error That Won't Go Away!! Please Help!!
2002-08-26 13:14:18 hembeck [View]
-
Manual Shutdown & Startup commands, for dummies
2002-08-21 10:51:09 hembeck [View]
-
Manual Shutdown & Startup commands, for dummies
2003-04-28 15:15:03 anonymous2 [View]
-
uninstall MySQL
2002-08-14 08:17:48 smackcell [View]
-
uninstall MySQL: obvious answer???
2002-08-14 10:53:45 smackcell [View]
-
PLEASE HELP!! Permissions again: Bind on unix
2002-08-03 09:56:07 macmad [View]
-
PLEASE HELP!! Permissions again: Bind on unix
2002-08-16 13:20:15 pathell [View]
-
errors when trying to create DB...
2002-07-28 08:49:44 ferrit [View]
-
errors when trying to create DB... sorted ;-)
2002-07-28 09:54:02 ferrit [View]
-
Can't login to mysql server
2002-07-25 07:48:18 besrouchon [View]
-
Can't login to mysql server
2003-04-14 13:22:31 anonymous2 [View]
-
Thanks!
2002-07-18 04:13:11 greenpeanut [View]
-
test.php does not work
2002-07-13 04:45:17 thegws [View]
-
test.php does not work
2004-01-04 13:32:46 anonymous2 [View]
-
test.php does not work
2003-08-28 18:13:55 anonymous2 [View]
-
test.php does not work
2002-07-23 23:17:01 macaddict [View]
-
test.php does not work
2002-07-19 12:38:10 placebo [View]
-
test.php does not work
2002-07-19 20:22:40 placebo [View]
-
What am I doing wrong?
2002-07-11 20:41:17 gregsixx [View]
-
Success - sort of
2002-07-11 21:13:22 gregsixx [View]
-
Okay, I think I've got it now
2002-07-17 22:23:03 gregsixx [View]
-
Uninstall MySQL
2002-07-01 01:41:35 axiomcreativesystems [View]
-
Can't start server: Bind on TCP/IP port: Address already in use
2002-06-30 03:25:38 mrx1 [View]
-
Can't start server: Bind on TCP/IP port: Address already in use
2002-07-12 09:52:02 sdpalmer [View]
-
Can't start server: Bind on TCP/IP port: Address already in use
2002-06-30 03:24:47 mrx1 [View]
-
auto startup for source install
2002-06-23 20:27:02 jrwilla [View]
-
test.php error
2002-06-23 16:48:11 bluesy [View]
-
test.php error
2002-06-25 06:14:37 bluesy [View]
-
Admin password changed
2002-06-19 12:42:01 pattern_p [View]
-
Permission Denied
2002-06-11 01:23:10 macette [View]
-
allmost same problem
2002-06-23 09:19:18 bluesy [View]
-
chown probs?
2002-06-10 00:37:49 hooplah [View]
-
chown probs?
2002-06-14 09:45:10 psmalera [View]
-
Finally Got It to Work!
2002-06-06 19:18:01 pogo [View]
-
error on test.php
2002-06-05 17:33:03 union144 [View]
-
error on test.php
2002-06-06 14:33:16 union144 [View]
-
MySQL must be have admin priveleges
2002-06-05 16:49:32 union144 [View]
-
Problems with decompression
2002-05-22 12:49:19 dhtmlkitchen [View]
-
Unable to define the MySQL User
2002-05-19 16:21:56 michele [View]
-
Yeah, but how do you shut it down
2002-05-10 17:33:27 hembeck [View]
-
.pkg MySQL installation won't start
2002-05-09 10:22:19 cmessley [View]
-
.pkg MySQL installation won't start
2002-05-10 10:47:03 cmessley [View]
-
Errcode: 13
2002-05-07 04:26:23 anthropos9 [View]
-
Apache giving 405 errors for POST requests.
2002-05-05 13:10:47 david.parrish@buentec.com [View]
-
Apache giving 405 errors for POST requests.
2003-07-18 17:27:48 anonymous2 [View]
-
Warning: MySQL Connection Failed: Can't connect to MySQL server on 'localhost' (61)
2002-05-04 17:21:56 xor81 [View]
-
update
2002-05-05 08:20:19 xor81 [View]
-
Can't Resolve Hostname
2002-05-01 09:13:09 orisjt [View]
-
access denied
2002-05-01 02:06:13 bernad [View]
-
access denied
2002-05-05 12:52:43 xor81 [View]
-
Ugh :: Warning: Access denied...
2002-05-01 00:31:56 gosick [View]
-
gosh-darn password stuff
2002-04-30 22:24:34 oldbenway [View]
-
gosh-darn password stuff
2002-10-09 11:08:16 thaddaeus [View]
-
gosh-darn password stuff
2005-03-21 17:07:53 JayBeeDee [View]
-
gosh-darn password stuff
2002-06-03 17:01:40 mknauf [View]
-
C compiler cannot create executables error
2002-04-27 17:01:09 dhtmlkitchen [View]
-
Problem with chown
2002-04-26 15:11:04 mac@udigit.se [View]
-
Incorrect chown statement
2002-04-26 00:16:18 clatko [View]
-
Incorrect chown statement
2002-04-26 04:44:03 Kevin Hemenway |
[View]
-
error 1044 and 1045
2002-04-25 20:21:48 gaga1 [View]
-
error 1044 and 1045
2002-04-26 04:38:38 Kevin Hemenway |
[View]
-
error 1044 and 1045
2008-09-18 03:13:35 riotxix [View]
-
error 1044 and 1045...ok..but no CREATE
2002-04-26 06:49:02 gaga1 [View]
-
error 1044 and 1045...ok..but no CREATE
2003-07-04 17:21:16 anonymous2 [View]
-
error 1044 and 1045...ok..but no CREATE
2002-04-26 18:34:12 Kevin Hemenway |
[View]
-
error 1044 and 1045...ok..but no CREATE
2002-04-26 20:49:58 gaga1 [View]
-
error 1044 and 1045...ok..but no CREATE
2002-04-26 14:46:01 venste [View]
-
error 1044 and 1045...BINGO!
2002-04-27 23:02:35 gaga1 [View]
-
error 1044 and 1045...BINGO!
2003-07-24 17:25:53 anonymous2 [View]
-
usernameerror 1044 and 1045...BINGO!
2003-06-25 21:41:34 anonymous2 [View]
-
access denied errors
2003-05-24 22:36:14 anonymous2 [View]
-
error 1044 and 1045...BINGO!
2003-05-02 17:03:09 anonymous2 [View]
-
error 1044 and 1045...BINGO!
2003-05-20 19:59:26 anonymous2 [View]
-
error 1044 and 1045...BINGO!
2003-05-24 22:42:24 anonymous2 [View]
-
error 1044 and 1045...BINGO!
2002-07-13 13:03:07 msimoes@umich.edu [View]
-
error 1044 and 1045...BINGO!
2002-10-02 19:24:57 anonymous2 [View]
-
Found a correction to final commands
2002-04-07 22:58:37 fleafrier [View]
-
uninstall mysql?
2002-04-05 23:12:44 vaie [View]
-
uninstall mysql?
2002-04-08 21:23:05 vaie [View]
-
Is it possible to uninstall source version?
2002-06-06 15:40:33 rdez378 [View]
-
What next?
2002-04-03 04:31:16 bovisart [View]
-
What next?
2002-04-27 18:34:11 dhtmlkitchen [View]
-
What next?
2002-04-16 17:29:36 Kevin Hemenway |
[View]
-
What next?
2002-04-27 18:31:13 dhtmlkitchen [View]
-
Can't connect .. compiled .. stuck on 2nd
2002-03-31 19:26:32 pangur [View]
-
Can't connect .. compiled .. stuck on 2nd
2002-04-01 20:53:47 pangur [View]
-
Access Denied error
2002-03-25 09:34:42 pedrox [View]
-
Access Denied error -ignore me
2002-03-25 09:51:57 pedrox [View]
-
Adobe Golive 6 - How To - Please...
2002-03-25 07:44:53 redleader [View]
-
Adobe Golive 6 - How To - Please...
2002-04-24 09:46:51 medazinol [View]
-
OT:: mod_perl
2002-03-24 21:39:53 kenyatta [View]
-
OT:: mod_perl
2002-03-29 14:10:36 Kevin Hemenway |
[View]
-
Darwin Streaming Server w/ Apache
2002-03-19 02:17:17 lauren1 [View]
-
still having trouble with cgi
2002-03-15 21:50:12 bdoltens [View]
-
still having trouble with cgi
2002-03-19 14:10:32 Kevin Hemenway |
[View]
-
mysqladmin: Command not found
2002-03-13 19:17:39 devereld [View]
-
mysqladmin: Command not found
2002-03-13 20:25:23 Kevin Hemenway |
[View]
-
MySQL won't start
2002-03-13 17:16:40 saywake [View]
-
MySQL won't start
2002-03-13 20:28:10 Kevin Hemenway |
[View]
-
MySQL won't start
2002-08-16 12:52:59 syn_raziel [View]
-
MySQL won't start
2005-01-20 16:26:14 jsswg0 [View]
-
MySQL won't start
2002-03-13 18:42:34 andycrim1 [View]
-
MySQL won't start - one more thing
2002-03-13 18:32:08 saywake [View]
-
MySQL won't start - one more thing
2002-03-13 20:30:31 Kevin Hemenway |
[View]
-
MySQL won't start - one more thing
2002-03-13 21:58:06 andycrim1 [View]
-
TOP NOTCH Articles
2002-03-13 13:45:17 dejesusivan [View]
-
what about jdbc driver?
2002-03-13 11:19:13 axel_vogelsang [View]
-
errcode: 13
2002-03-13 08:09:59 andycrim1 [View]
-
errcode: 13
2002-03-13 20:32:02 Kevin Hemenway |
[View]
-
Do it over again with Perl
2002-03-12 12:56:52 dicklacara [View]
-
Do it over again with Perl
2002-03-13 20:51:56 Kevin Hemenway |
[View]
-
Do it over again with Perl
2002-03-18 19:45:57 gskluzacek [View]
-
Do it over again with Perl
2002-03-19 14:04:21 Kevin Hemenway |
[View]
-
FastCGI on OS X?
2002-03-11 19:59:41 juicy [View]
-
MySQL problems
2002-03-11 18:24:34 spiffyman [View]
-
MySQL problems
2002-06-30 04:19:01 mrx1 [View]
-
PHP again
2002-03-11 14:55:50 bici [View]
-
PHP again
2002-03-11 16:42:58 Kevin Hemenway |
[View]
-
php
2002-03-11 14:51:15 bici [View]
-
php
2002-03-11 16:41:22 Kevin Hemenway |
[View]
-
OSXGuide.com
2002-03-11 07:09:06 dani0100 [View]
-
"Can't connect to mysql.."
2002-03-10 05:00:04 dbartlett [View]
-
"Can't connect to mysql.."
2002-03-13 21:35:47 rjolly@cape.com [View]
-
"Can't connect to mysql.."
2003-05-07 21:11:33 jcotton [View]
-
"Can't connect to mysql.."
2002-03-11 08:17:45 Kevin Hemenway |
[View]
-
And Mac OS X Server?
2002-03-09 07:03:08 maxs [View]
-
And Mac OS X Server?
2002-03-11 08:14:31 Kevin Hemenway |
[View]
-
And Mac OS X Server?
2002-04-29 09:13:24 cybersank [View]
-
One missing sudo?
2002-03-08 22:14:44 pmccann [View]
-
One missing sudo?
2002-03-11 08:12:12 Kevin Hemenway |
[View]

