Annotation of doc/build/loncapasqldatabase.html, revision 1.6
1.1 harris41 1: <HTML>
2: <HEAD>
3: <TITLE>LON-CAPA SQL Database Documentation</TITLE>
4: </HEAD>
5: <BODY>
6: <H1>LON-CAPA SQL Database Documentation</H1>
7: <P>
8: Scott Harrison
9: </P>
10: <P>
1.5 harris41 11: Last updated: 02/12/2001
1.1 harris41 12: </P>
1.2 harris41 13: <P>
1.1 harris41 14: This file describes issues associated with LON-CAPA
15: and a SQL database.
16: </P>
1.5 harris41 17: <H2>Latest HOWTO</H2>
1.2 harris41 18: <P>
1.4 harris41 19: <UL>
20: <LI>Current status of documentation</LI>
21: <LI>Current status of implementation</LI>
22: <LI>Purpose within LON-CAPA</LI>
23: <LI>Installation</LI>
24: <LI>Installation from source</LI>
25: <LI>Configuration (automated)</LI>
26: <LI>Manual configuration</LI>
27: <LI>Testing</LI>
28: <LI>Example sections of code relevant to LON-CAPA</LI>
29: </UL>
30: </P>
1.5 harris41 31: <H2>Current status of documentation</H2>
1.4 harris41 32: <P>
1.2 harris41 33: I am going to begin documentation by inserting what notes
34: I have into this file. I will be subsequently rearranging
1.4 harris41 35: them and editing them based on the tests that I conduct.
1.2 harris41 36: I am trying to make sure that documentation, installation,
37: and run-time issues are all consistent and correct. The
38: current status of everything is that it works and has
39: been minimally tested, but things need to be cleaned up
40: and checked again!
41: </P>
1.5 harris41 42: <H2>Current status of implementation</H2>
1.4 harris41 43: <P>
1.6 ! harris41 44: Need to
! 45: <UL>
! 46: <LI>Installation: Fix binary file listings for user permissions and ownership.
! 47: <LI>Installation: Make sure sql server starts, and if database does not
! 48: exist, then create. (/etc/rc.d).
! 49: <LI>Processes: Make sure loncron initiates lonsql on library machines.
! 50: <LI>Read in metadata from right place periodically.
! 51: <LI>Implement tested perl module handler.
! 52: </UL>
! 53: <P>
1.4 harris41 54: Right now, a lot of "feasibility" work has been done.
55: Recipes for manual installation and configuration have
56: been gathered. Network connectivity of lond->lonsql->lond->lonc
57: type tests have been performed. A binary installation
58: has been compiled in an RPM (LON-CAPA-mysql).
59: The most lacking test in terms of feasibility has
60: been looking at benchmarks to analyze the load at which
61: the SQL database can efficiently allow many users to
62: make simultaneous requests of the metadata database.
63: </P>
64: <P>
65: Documentation has been pieced together over time. But,
66: as mentioned in the previous section, it needs an
67: overhaul.
68: </P>
69: <P>
70: The binary installation has some quirks associated with it.
71: Some of the user permissions are wrong, although this is
72: benign. Also, other options of binary installation (such
73: as using binary RPMs put together by others) were dismissed
74: given the difficulty of getting differing combinations of
75: these external RPMs to work together.
76: </P>
77: <P>
78: Most configuration questions have been initially worked out
79: to the point of getting this SQL software component working,
80: however there may be more optimal approaches than currently
81: exist.
82: </P>
1.5 harris41 83: <H2>Purpose within LON-CAPA</H2>
1.4 harris41 84: <P>
85: LON-CAPA is meant to distribute A LOT of educational content
86: to A LOT of people. It is ineffective to directly rely on contents
87: within the ext2 filesystem to be speedily scanned for
88: on-the-fly searches of content descriptions. (Simply put,
89: it takes a cumbersome amount of time to open, read, analyze, and
90: close thousands of files.)
91: </P>
92: <P>
93: The solution is to hash-index various data fields that are
94: descriptive of the educational resources on a LON-CAPA server
95: machine. Descriptive data fields are referred to as
96: "metadata". The question then arises as to how this metadata
97: is handled in terms of the rest of the LON-CAPA network
98: without burdening client and daemon processes. I now
99: answer this question in the format of Problem and Solution
100: below.
101: </P>
102: <P>
103: <PRE>
104: PROBLEM SITUATION:
105:
106: If Server A wants data from Server B, Server A uses a lonc process to
107: send a database command to a Server B lond process.
108: lonc= loncapa client process A-lonc= a lonc process on Server A
109: lond= loncapa daemon process
110:
111: database command
112: A-lonc --------TCP/IP----------------> B-lond
113:
114: The problem emerges that A-lonc and B-lond are kept waiting for the
115: MySQL server to "do its stuff", or in other words, perform the conceivably
116: sophisticated, data-intensive, time-sucking database transaction. By tying
117: up a lonc and lond process, this significantly cripples the capabilities
118: of LON-CAPA servers.
119:
120: While commercial databases have a variety of features that ATTEMPT to
121: deal with this, freeware databases are still experimenting and exploring
122: with different schemes with varying degrees of performance stability.
123:
124: THE SOLUTION:
125:
126: A separate daemon process was created that B-lond works with to
127: handle database requests. This daemon process is called "lonsql".
128:
129: So,
130: database command
131: A-lonc ---------TCP/IP-----------------> B-lond =====> B-lonsql
132: <---------------------------------/ |
133: "ok, I'll get back to you..." |
134: |
135: /
136: A-lond <------------------------------- B-lonc <======
137: "Guess what? I have the result!"
138:
139: Of course, depending on success or failure, the messages may vary,
140: but the principle remains the same where a separate pool of children
141: processes (lonsql's) handle the MySQL database manipulations.
142: </PRE>
143: </P>
1.5 harris41 144: <H2>Installation</H2>
1.4 harris41 145: <P>
146: Installation of the LON-CAPA SQL database normally occurs
147: by default when using the LON-CAPA installation CD
148: (see http://install.lon-capa.org). It is installed
149: as the LON-CAPA-mysql RPM. This RPM encodes for the MySQL
150: engine and related perl interfaces (Perl::DBI, Perl::Msql-Mysql).
151: </P>
152: <P>
153: The three components of a MySQL installation for the
154: LON-CAPA system are further described immediately below.
155: <TABLE BORDER="0">
156: <TR><TD COLSPAN="2"><STRONG>Perl::DBI module</STRONG>-
157: the API "front-end"...</TD></TR>
158: <TR><TD WIDTH="10%"></TD><TD>database interface module for organizing generic
159: database commands which are independent of specific
160: database implementation (such as MySQL, mSQL, Postgres, etc).
161: </TD></TR>
162: <TR><TD COLSPAN="2"><STRONG>Perl::MySQL module</STRONG>-
163: the API "mid-section"...</TD></TR>
164: <TR><TD WIDTH="10%"></TD><TD>the module to directly interface with the actual
165: MySQL database engine</TD></TR>
166: <TR><TD COLSPAN="2"><STRONG>MySQL database engine</STRONG>-
167: the "back-end"...</TD></TR>
168: <TR><TD WIDTH="10%"></TD><TD>the binary installation (compiled either
169: from source or pre-compiled file listings) which provides the
170: actual MySQL functionality on the system</TD></TR>
171: </TABLE>
172: </P>
1.5 harris41 173: <H2>Installation from source</H2>
1.4 harris41 174: <P>
175: The following set of tarballs was found to work together
176: properly on a LON-CAPA RedHat 6.2 system:
177: <UL>
178: <LI>DBI-1.13.tar.gz
179: <LI>Msql-Mysql-modules-1.2209.tar.gz
180: <LI>mysql-3.22.32.tar.gz
181: </UL>
182: </P>
183: <P>
184: Installation was simply a matter of following the instructions
185: and typing the several "make" commands for each
186: </P>
1.5 harris41 187: <H2>Configuration (automated)</H2>
1.4 harris41 188: <P>
189: Not yet developed. This will be part of an interface
190: present on LON-CAPA systems that can be launched by
191: entering the command <TT>/usr/sbin/loncapaconfig</TT>.
192: </P>
1.5 harris41 193: <H2>Manual configuration</H2>
1.4 harris41 194: <P>
195: This is not complete.
196: </P>
197: <P>
198: <STRONG>Starting the mysql daemon</STRONG>: Login on the Linux
199: system as user 'www'. Enter the command
200: <TT>/usr/local/bin/safe_mysqld &</TT>
201: </P>
202: <P>
203: <STRONG>Set a password for 'root'</STRONG>:
204: <TT>/usr/local/bin/mysqladmin -u root password 'new-password'</TT>
205: </P>
206: <P>
207: <STRONG>Adding a user</STRONG>: Start the mysql daemon. Login to the
208: mysql system as root (<TT>mysql -u root -p mysql</TT>)
209: and enter the right password (for instance 'newmysql'). Add the user
210: www
211: <PRE>
212: INSERT INTO user (Host, User, Password)
213: VALUES ('localhost','www',password('newmysql'));
214: </PRE>
215: </P>
216: <P>
217: <STRONG>Granting privileges to user 'www'</STRONG>:
218: <PRE>
219: GRANT ALL PRIVILEGES ON *.* TO www@localhost;
220: FLUSH PRIVILEGES;
221: </PRE>
222: </P>
223: <P>
224: <STRONG>Set the SQL server to start upon system startup</STRONG>:
225: Copy support-files/mysql.server to the right place on the system
226: (/etc/rc.d/...).
227: </P>
1.1 harris41 228: <P>
1.5 harris41 229: <STRONG>The Perl API</STRONG>
1.2 harris41 230: <PRE>
231: $dbh = DBI->connect( "DBI:mysql:loncapa",
232: "www",
233: "SOMEPASSWORD",
234: { RaiseError =>0,PrintError=>0});
235:
236: There is an obvious need to CONNECT to the database, and in order to do
237: this, there must be:
238: a RUNNING mysql daemon;
239: a DATABASE named "loncapa";
240: a USER named "www";
241: and an ABILITY for LON-CAPA on one machine to access
242: SQL database on another machine;
243:
244: So, here are some notes on implementing these configurations.
245:
246: ** RUNNING mysql daemon (safe_mysqld method)
247:
248: The recommended way to run the MySQL daemon is as a non-root user
249: (probably www)...
250:
251: so, 1) login as user www on the linux machine
252: 2) start the mysql daemon as /usr/local/bin/safe_mysqld &
253:
254: safe_mysqld only works if the local installation of MySQL is set to the
255: right directory permissions which I found to be:
256: chown www:users /usr/local/var/mysql
257: chown www:users /usr/local/lib/mysql
258: chown -R www:users /usr/local/mysql
259: chown www:users /usr/local/include/mysql
260: chown www:users /usr/local/var
261:
262: ** DATABASE named "loncapa"
263:
264: As user www, run this command
265: mysql -u root -p mysql
266: enter the password as SOMEPASSWORD
267:
268: This allows you to manually enter MySQL commands.
269: The MySQL command to generate the loncapa DATABASE is:
270:
271: CREATE DATABASE 'loncapa';
272:
273: ** USER named "www"
274:
275: As user www, run this command
276: mysql -u root -p mysql
277: enter the password as SOMEPASSWORD
278:
279: To add the user www to the MySQL server, and grant all
280: privileges on *.* to www@localhost identified by 'SOMEPASSWORD'
281: with grant option;
282:
283: INSERT INTO user (Host, User, Password)
284: VALUES ('localhost','www',password('SOMEPASSWORD'));
285:
286: GRANT ALL PRIVILEGES ON *.* TO www@localhost;
287:
288: FLUSH PRIVILEGES;
289:
290: ** ABILITY for LON-CAPA machines to communicate with SQL databases on
291: other LON-CAPA machines
292:
1.5 harris41 293: An up-to-date lond and lonsql.
294: </PRE>
295: </P>
296: <H2>Testing</H2>
297: <P>
298: <PRE>
299: <STRONG>** TEST the database connection with my current tester.pl code
300: which mimics what command will eventually be sent through lonc.</STRONG>
1.2 harris41 301:
1.5 harris41 302: $reply=reply(
303: "querysend:SELECT * FROM general_information WHERE Id='AAAAA'",$lonID);
304: </PRE>
305: </P>
306: <H2>Example sections of code relevant to LON-CAPA</H2>
307: <P>
1.2 harris41 308: Here are excerpts of code which implement the above handling:
1.5 harris41 309: </P>
310: <P>
311: <PRE>
312: <STRONG>**LONSQL
1.2 harris41 313: A subroutine from "lonsql" which establishes a child process for handling
1.5 harris41 314: database interactions.</STRONG>
1.2 harris41 315:
316: sub make_new_child {
317: my $pid;
318: my $sigset;
319:
320: # block signal for fork
321: $sigset = POSIX::SigSet->new(SIGINT);
322: sigprocmask(SIG_BLOCK, $sigset)
323: or die "Can't block SIGINT for fork: $!\n";
324:
325: die "fork: $!" unless defined ($pid = fork);
326:
327: if ($pid) {
328: # Parent records the child's birth and returns.
329: sigprocmask(SIG_UNBLOCK, $sigset)
330: or die "Can't unblock SIGINT for fork: $!\n";
331: $children{$pid} = 1;
332: $children++;
333: return;
334: } else {
335: # Child can *not* return from this subroutine.
336: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
337:
338: # unblock signals
339: sigprocmask(SIG_UNBLOCK, $sigset)
340: or die "Can't unblock SIGINT for fork: $!\n";
341:
342:
343: #open database handle
344: # making dbh global to avoid garbage collector
345: unless (
346: $dbh = DBI->connect("DBI:mysql:loncapa","www","SOMEPASSWORD",{ RaiseError =>0,PrintError=>0})
347: ) {
348: my $st=120+int(rand(240));
349: &logthis("<font color=blue>WARNING: Couldn't connect to database ($st secs): $@</font>");
350: print "database handle error\n";
351: sleep($st);
352: exit;
353:
354: };
355: # make sure that a database disconnection occurs with ending kill signals
356: $SIG{TERM}=$SIG{INT}=$SIG{QUIT}=$SIG{__DIE__}=\&DISCONNECT;
357:
358: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
359: for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
360: $client = $server->accept() or last;
361:
362: # do something with the connection
363: $run = $run+1;
364: my $userinput = <$client>;
365: chomp($userinput);
366:
367: my ($conserver,$querytmp)=split(/&/,$userinput);
368: my $query=unescape($querytmp);
369:
370: #send query id which is pid_unixdatetime_runningcounter
371: $queryid = $thisserver;
372: $queryid .="_".($$)."_";
373: $queryid .= time."_";
374: $queryid .= $run;
375: print $client "$queryid\n";
376:
377: #prepare and execute the query
378: my $sth = $dbh->prepare($query);
379: my $result;
380: unless ($sth->execute())
381: {
382: &logthis("<font color=blue>WARNING: Could not retrieve from database: $@</font>");
383: $result="";
384: }
385: else {
386: my $r1=$sth->fetchall_arrayref;
387: my @r2; map {my $a=$_; my @b=map {escape($_)} @$a; push @r2,join(",", @b)} (@$r1);
388: $result=join("&",@r2) . "\n";
389: }
390: &reply("queryreply:$queryid:$result",$conserver);
391:
392: }
393:
394: # tidy up gracefully and finish
395:
396: #close the database handle
397: $dbh->disconnect
398: or &logthis("<font color=blue>WARNING: Couldn't disconnect from database $DBI::errstr ($st secs): $@</font>");
399:
400: # this exit is VERY important, otherwise the child will become
401: # a producer of more and more children, forking yourself into
402: # process death.
403: exit;
404: }
405: }
1.5 harris41 406: </P>
407: <P>
408: <STRONG>** LOND enabling of MySQL requests</STRONG>
409: <BR />
410: This code is part of every lond child process in the
411: way that it parses command request syntax sent to it
412: from lonc processes. Based on the diagram above, querysend
413: corresponds to B-lonc sending the result of the query.
414: queryreply corresponds to B-lond indicating that it has
415: received the request and will start the database transaction
416: (it returns "ok" to
417: A-lonc ($client)).
418: <PRE>
1.2 harris41 419: # ------------------------------------------------------------------- querysend
420: } elsif ($userinput =~ /^querysend/) {
421: my ($cmd,$query)=split(/:/,$userinput);
422: $query=~s/\n*$//g;
423: print $client sqlreply("$hostid{$clientip}\&$query")."\n";
424: # ------------------------------------------------------------------ queryreply
425: } elsif ($userinput =~ /^queryreply/) {
426: my ($cmd,$id,$reply)=split(/:/,$userinput);
427: my $store;
428: my $execdir=$perlvar{'lonDaemons'};
429: if ($store=IO::File->new(">$execdir/tmp/$id")) {
430: print $store $reply;
431: close $store;
432: print $client "ok\n";
433: }
434: else {
435: print $client "error:$!\n";
436: }
437:
1.5 harris41 438: </PRE>
1.2 harris41 439:
1.1 harris41 440: </P>
441: </BODY>
1.3 harris41 442: </HTML>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>