How to view executed SQL

How to view executed SQL

by Lewis Kapell -
Number of replies: 2

If I turn on debugging in Moodle 3.0, will it show me all the SQL queries that are being executed?  Is there some other tool within Moodle to do this?  Or would I need to use tools within my database (Oracle) to show me the statements that have been executed?

I want to learn about the database structure by performing certain actions (creating a course, enrolling users, etc.) and then see what SQL statements are being executed.

Thank you.

Average of ratings: -
In reply to Lewis Kapell

Re: How to view executed SQL

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can turn on SQL output by editing the PHP code for a page and adding:

 $DB->set_debug(true);

You are likely to get a lot of output from this, depending on the page you enable it on.

Average of ratings: Useful (1)
In reply to Lewis Kapell

Re: How to view executed SQL

by Ken Task -
Picture of Particularly helpful Moodlers

Davo's way is perhaps best for true programmers.   There is another thang one could do:

one could setup mysql to log, have mysql log queries, then tell mysql to turn off logging.   What ever is done when logging is on is seen in logs.

Don't leave it on or those could easily fill up a lot of space on DB server.

Settings in my.cnf

# for logging
general_log_file=/var/log/mysql/mysql-general.log
slow_query_log_file=/var/log/mysql/mysql-slow.log

One would have to create the log files (which contain nothing at first):

touch /var/log/mysql/mysql-slow.log
and
touch /var/log/mysql/mysql-general.log

Then set their ownerships/permissions for the mysqld user

chown mysql:mysql /var/log/mysql/mysql-slow.log
chown mysql:mysql /var/log/mysql/mysql-general.log

so that mysql can write to them

# to turn on one or both:
mysql -e 'set global log = 1' -u root -p'[password]'
mysql -e 'set global log_slow_queries = 1' -u root -p'password'
mysql -e 'set global slow_query_log = 1' -u root -p'password'

If you install multitail one could watch those logs in realtime.

multitail -i /var/log/mysql/mysql-general.log -i /var/log/mysql/mysql-slow.log -i /var/log/httpd/access_log -i /var/log/httpd/error_log;

# to turn off
# set global slow_query_log = 0
# set global log_slow_queries = 0
# set global log = 0;

Once off, those logs can be copied out of /var/log/mysql/ saved as a text file
(they are ascii anyway), download them and inspect with notepad/textedit or
any text editor

'spirit of sharing', Ken


Average of ratings: Useful (2)