Set Up MySQL Database for Moodle in Webmin
In this article
To set up a MySQL database for Moodle in Webmin, you create a database and a dedicated user, assign privileges, and note the credentials for Moodle's config.php. This guide walks through the exact steps in Webmin's MySQL module. You'll need root or sudo access to Webmin and a running MySQL server (Webmin's module works with MySQL or MariaDB).
What You'll Need
- Webmin installed and accessible (usually at
https://your-server:10000). - Root login or a user with sudo privileges for Webmin.
- MySQL or MariaDB server installed and running on the same or a reachable host.
- Moodle files already uploaded to your web server (e.g., in
/var/www/html/moodle).
Step-by-Step
- Log into Webmin using your browser. Enter your server's IP or hostname followed by
:10000(e.g.,https://192.168.1.10:10000). Accept the self-signed certificate warning if it appears. - Open the MySQL Database Server module. In the left sidebar, under Servers, click MySQL Database Server. If you don't see it, check that MySQL is installed and the Webmin module is enabled (look under Un-used Modules).
- Create a new database. Click Create a new database (or Create new database). In the Database name field, enter something like
moodle_db. Choose the default character set – utf8mb4 is recommended for Moodle. Leave the other options as default unless you know you need something else. Click Create. - Create a database user. After creating the database, go back to the MySQL module main page and click Create a new user (or Create user). Fill in a username (e.g.,
moodle_user) and a strong password. Under Host, choose Localhost if Moodle runs on the same server, or enter the web server's IP if it's remote. Click Create. - Grant privileges to the user. In the user list, click on your newly created user. Under Privileges, select All privileges (or check them individually – Moodle needs SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, and more). For simplicity, choose All and then click Save (or Apply).
- Add the user to the database. Still in the user editing page, scroll to the Databases section. Select your
moodle_dbfrom the list and click Add (or Add to database). This links the user to the database with the privileges you set. Click Save again. - Verify the connection. To test, go to the MySQL module's main page and click Execute SQL (or Command). Run a simple query like
SHOW DATABASES;to confirm the server is responsive. You can also test the user login by using the MySQL client from the command line on your server:mysql -u moodle_user -p moodle_db. - Record your credentials. Write down the database name (
moodle_db), username (moodle_user), password, and host (usuallylocalhost). You'll need these during Moodle's installation. - Proceed with Moodle installation. When you run Moodle's web installer (e.g.,
http://your-server/moodle), choose MySQL Improved (mysqli) as the driver. Enter the database host, name, user, and password you just created. Moodle will then create its tables automatically.
Common Mistakes
- Using the wrong character set: Moodle requires
utf8mb4(or at leastutf8). If you create the database withlatin1, you'll get errors about collation. Always chooseutf8mb4. - Forgetting to grant privileges: If the user has no rights to the database, Moodle will fail with a “connection to database failed” error. Double-check that the user is added to the database with privileges.
- Host mismatch: If Moodle is on a different server, setting the user's host to
localhostwill block remote access. Use the web server's IP or%(but be cautious with%as it allows any host). - Not using mysqli: Moodle expects the
mysqliPHP extension. If you use an oldmysqldriver, it won't work. Ensure your PHP hasmysqlienabled.
Tips
- Use a strong password for the database user – Moodle stores it in plain text in
config.php, so protect that file. - Back up your database regularly. In Webmin, you can use the Backup Database option in the MySQL module to schedule dumps.
- If you're using MariaDB instead of MySQL, the Webmin module works the same way – just ensure the service is running.
- For better security, create a separate user for Moodle rather than using the root MySQL account.
Troubleshooting
Moodle says “Cannot connect to database” – Check that the MySQL service is running (systemctl status mysql or service mysql status). Verify the host, username, and password are correct. Test the connection from the command line: mysql -u moodle_user -p moodle_db. If that fails, re-check the user's host setting.
Error about collation or character set – Run ALTER DATABASE moodle_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; in Webmin's SQL command box, then retry the Moodle install.
Permission denied when creating tables – The user needs CREATE and ALTER privileges. Go back to the user's privilege settings in Webmin and grant all privileges on the database.
Webmin shows “MySQL module not found” – Install the module via Webmin's Module Configuration or use your OS package manager (e.g., apt install webmin-mysql on Debian/Ubuntu).
If all else fails – Restart MySQL (systemctl restart mysql) and restart your web server to ensure PHP picks up the mysqli extension. Also check the PHP error logs (/var/log/php_errors.log or similar) for clues.
Frequently Asked Questions
Can I use an existing database for Moodle?
Yes, but it's safer to create a fresh one to avoid conflicts. If you reuse an existing database, make sure it's empty or that Moodle's tables won't clash with existing ones.
What is the difference between MySQL and MariaDB in Webmin?
Webmin's MySQL module supports both. MariaDB is a drop-in replacement for MySQL, so the setup steps are identical. Just ensure the service name is correct (e.g., mariadb vs mysql).
Do I need to create a database user manually if I'm using the root account?
You can use the root account, but it's not recommended for security. Moodle's config.php would contain root credentials, which is a risk. Always create a dedicated user with limited privileges.
How do I find the MySQL host for Moodle if it's on the same server?
Use localhost or 127.0.0.1. If your web server and database are on different machines, use the database server's IP address.
What if I get an error about the mysqli extension not being installed?
Install the PHP MySQL extension for your OS. On Ubuntu/Debian: sudo apt install php-mysql. On CentOS/RHEL: sudo yum install php-mysqlnd. Then restart your web server.
Can I change the database name after Moodle is installed?
Yes, but it's not trivial. You'd need to update config.php and move the data. It's easier to choose a clear name from the start.