PHP modules and MySQL databases

Print Friendly, PDF & Email

The goal here is to get our PHP scripts from Puma working on Tempest.  We want the PHP modules on Tempest to duplicate those on Puma, so that all the PHP scripts will run correctly.  We also need to check on the php.ini file and any other configuration info.

I intended to copy the /etc/php.ini file from puma, but that one is for an older version of PHP, so I think it will make more sense to edit the copy on Tempest. As with httpd.conf and such, I’m going to put our master copy in /home/sysadmin/etc/php.ini and use the new update-etc-file to maintain the one on Tempest.

I made two modifications to /etc/php.ini:

[root@tempest conf.d]# diff /etc/php.ini /etc/php.ini.orig 
530,533c530
< ; Wellesley modification
< ; display_errors = Off
< display_errors = On
< 
---
> display_errors = Off
685,687c682
< ; Wellesley Mod.  I think this means cookies override GET and POST, not vice versa
< ; request_order = "GP"
< request_order = "GPC"
---
> request_order = "GP"

Okay, now I had installed MDB2, but when I run, for example, http://tempest.wellesley.edu/~sysadmin/sysadmin/machines-dump.php I’m getting:

Error while connecting : MDB2 Error: not found

I’m not sure what’s causing that.  If I run PHP and the script from the command line, what do I get?

[root@tempest conf.d]# cd /home/sysadmin/public_html/sysadmin/
[root@tempest sysadmin]# php machines-dump.php 
<html>
<head>
<title>CS Department Machines</title>
</head>

<body bgcolor="white">

<p><strong>Failed on query 
<q>SELECT name,location,status,repl,sn,hwaddr,model,ip,misc FROM machines 
WHERE status <> 'dead' ORDER BY name,name ASC</q> 
with error message: MDB2 Error: connect failed</strong>
[root@tempest sysadmin]#

Okay, so it’s not that the MDB2 module is missing; it’s not able to connect to the database.  The MDB2-functions and sysadmin-dsn.inc files are there.  Let’s try connecting by hand:

[root@tempest sysadmin]# mysql -u sysadmin
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
[root@tempest sysadmin]# service mysqld status
mysqld is stopped
[root@tempest sysadmin]# chkconfig mysqld --list
mysqld             0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@tempest sysadmin]#

Oh, well, that’ll do it.  Let’s start it, and ensure that it always starts at boot time:

[root@tempest sysadmin]# chkconfig mysqld on
[root@tempest sysadmin]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@tempest sysadmin]# mysql -u sysadmin -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'sysadmin'@'localhost' (using password: YES)

Okay, what’s up with the sysadmin account:

[root@tempest sysadmin]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> select user from mysql.user;
+------+
| user |
+------+
| root |
| root |
+------+
2 rows in set (0.00 sec)

mysql>

Okay, so I didn’t copy any of the users over from Puma.  That’s okay.  We should probably archive the old users and their accounts.  Ideally, I think we should put the mysqldump of someone’s account in their home directory, so that they stay together, say in backups and such.  But then we have to deal with quota issues and such.  Right now, the mysqldumps are being put in /usr/network/sysadmin/mysqldumps, which is convenient for copying it over to Tempest, but not ideal for security/privacy.

I wrote a script migrate-mysql-user which dumps out the grants to a file and copies that file to /root/tmp on tempest.  I tried that, but it failed because the sysadmin database, which we are granting access to, doesn’t exist.  So, I created that database very easily:

[root@tempest sysadmin]# mysql <  /usr/network/sysadmin/mysqldumps/sysadmin.sql

(To be honest, I went back to revise the mysql-backup-multi script, which is run nightly by cron, to add the CREATE DATABASE statement at the top of the dump.  That facilitates rebuilding a broken database and also for migrating, as in this case.

Then, create the user:

[root@tempest tmp]# more sysadmin-user.sql 
GRANT USAGE ON *.* TO 'sysadmin'@'localhost' IDENTIFIED BY PASSWORD '*B83F1E63F975825B10548FC4243FAB842E204B4B'
GRANT ALL PRIVILEGES ON `sysadmin`.* TO 'sysadmin'@'localhost'

[root@tempest tmp]# mysql < /root/tmp/sysadmin-user.sql 
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GRANT ALL PRIVILEGES ON `sysadmin`.* TO 'sysadmin'@'localhost'' at line 2
[root@tempest tmp]#

Darn!  This is exactly the syntax that mysql on Puma generated.  Is there a version difference?

[root@tempest tmp]# mysql -V
mysql  Ver 14.14 Distrib 5.1.61, for redhat-linux-gnu (x86_64) using readline 5.1
[root@tempest tmp]# ssh puma mysql -V
mysql  Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1
[root@tempest tmp]#

Not much of a difference, so that seems unlikely.

Hang on, the error message indicates that the syntax error is noticed at the very first word, namely “grant all …”  Ah, and I notice that the previous statement doesn’t end in a semi-colon.  Could it be as simple and obvious as that?  I’ll add the semi-colons and try again:

[root@tempest tmp]# cat sysadmin-user.sql
GRANT USAGE ON *.* TO 'sysadmin'@'localhost' IDENTIFIED BY PASSWORD '*B83F1E63F975825B10548FC4243FAB842E204B4B';
GRANT ALL PRIVILEGES ON `sysadmin`.* TO 'sysadmin'@'localhost';
[root@tempest tmp]# mysql < /root/tmp/sysadmin-user.sql

Success!  Still, that’s annoying.  We’ll have to try to figure out a programmatic way to add those semi-colons.  Ah, we can do that with sed:

[root@tempest tmp]# cat sysadmin-user.sql~ | sed 's/$/;/'
GRANT USAGE ON *.* TO 'sysadmin'@'localhost' IDENTIFIED BY PASSWORD '*B83F1E63F975825B10548FC4243FAB842E204B4B';
GRANT ALL PRIVILEGES ON `sysadmin`.* TO 'sysadmin'@'localhost';
;

Let’s migrate the “radmin” mysql user (which is actually the correct user for the script) and go back and try our PHP script:

root@tempest sysadmin]# mysql < /root/tmp/radmin-user.sql 
[root@tempest sysadmin]# php machines-dump.php 
<html>
<head>
<title>CS Department Machines</title>
</head>

<!-- Written by Scott D. Anderson, Summer 2004.  Rewritten to use PEAR, summer 2008.  scott.anderson@acm.org -->

<body bgcolor="white">

<table border='1' cellpadding='10'>
<tr>
<th><a href="machines-dump.php?sortkey=name">name</a></th><th><a href="machines-dump.php?sortkey=location">location</a></th><th><a href="machines-dump.php?sortkey=status">status</a></th><th><a href="machines-dump.php?sortkey=repl">repl</a></th><th><a href="machines-dump.php?sortkey=sn">sn</a></th><th><a href="machines-dump.php?sortkey=hwaddr">hwaddr</a></th><th><a href="machines-dump.php?sortkey=model">model</a></th><th><a href="machines-dump.php?sortkey=ip">ip</a></th><th><a href="machines-dump.php?sortkey=misc">misc</a></th></tr>
<tr bgcolor="CCCCCC"><td>baboon</td><td>E125</td><td>ok</td><td>2011-02</td><td><a href="machine-update.php?sn=MJFNLZ9
...

Okay, success!  Woo hoo!

 

 

About CS SysAdmins

The CS Department System Administrators
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *