<h1>Choosing An SQL Server<aclass="headerlink"href="#choosing-an-sql-server"title="Permalink to this headline">¶</a></h1>
<p>This page gives an overview of the supported SQL databases as well as instructions on install:</p>
<ulclass="simple">
<li><p>SQLite3 (default)</p></li>
<li><p>PostgreSQL</p></li>
<li><p>MySQL / MariaDB</p></li>
</ul>
<p>Since Evennia uses <aclass="reference external"href="http://djangoproject.com">Django</a>, most of our notes are based off of what we know from the community and their documentation. While the information below may be useful, you can always find the most up-to-date and “correct” information at Django’s <aclass="reference external"href="http://docs.djangoproject.com/en/dev/ref/databases/#ref-databases">Notes about supported Databases</a> page.</p>
<divclass="section"id="sqlite3">
<h2>SQLite3<aclass="headerlink"href="#sqlite3"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference external"href="https://sqlite.org/">SQLite3</a> is a light weight single-file database. It is our default database and Evennia will set this up for you automatically if you give no other options. SQLite stores the database in a single file (<codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/evennia.db3</span></code>). This means it’s very easy to reset this database - just delete (or move) that <codeclass="docutils literal notranslate"><spanclass="pre">evennia.db3</span></code> file and run <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span><spanclass="pre">migrate</span></code> again! No server process is needed and the administrative overhead and resource consumption is tiny. It is also very fast since it’s run in-memory. For the vast majority of Evennia installs it will probably be all that’s ever needed.</p>
<p>SQLite will generally be much faster than MySQL/PostgreSQL but its performance comes with two drawbacks:</p>
<ulclass="simple">
<li><p>SQLite <aclass="reference external"href="https://www.sqlite.org/faq.html#q9">ignores length constraints by design</a>; it is possible to store very large strings and numbers in fields that technically should not accept them. This is not something you will notice; your game will read and write them and function normally, but this <em>can</em> create some data migration problems requiring careful thought if you do need to change databases later.</p></li>
<li><p>SQLite can scale well to storage of millions of objects, but if you end up with a thundering herd of users trying to access your MUD and web site at the same time, or you find yourself writing long-running functions to update large numbers of objects on a live game, either will yield errors and interference. SQLite does not work reliably with multiple concurrent threads or processes accessing its records. This has to do with file-locking clashes of the database file. So for a production server making heavy use of process- or thread pools (or when using a third-party webserver like Apache), a proper database is a more appropriate choice.</p></li>
</ul>
<divclass="section"id="install-of-sqlite3">
<h3>Install of SQlite3<aclass="headerlink"href="#install-of-sqlite3"title="Permalink to this headline">¶</a></h3>
<p>This is installed and configured as part of Evennia. The database file is created as <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/evennia.db3</span></code> when you run</p>
<p>without changing any database options. An optional requirement is the <codeclass="docutils literal notranslate"><spanclass="pre">sqlite3</span></code> client program - this is required if you want to inspect the database data manually. A shortcut for using it with the evennia database is <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span><spanclass="pre">dbshell</span></code>. Linux users should look for the <codeclass="docutils literal notranslate"><spanclass="pre">sqlite3</span></code> package for their distro while Mac/Windows should get the <aclass="reference external"href="https://sqlite.org/download.html">sqlite-tools package from this page</a>.</p>
<p>To inspect the default Evennia database (once it’s been created), go to your game dir and do</p>
<p>This will bring you into the sqlite command line. Use <codeclass="docutils literal notranslate"><spanclass="pre">.help</span></code> for instructions and <codeclass="docutils literal notranslate"><spanclass="pre">.quit</span></code> to exit. See <aclass="reference external"href="https://gist.github.com/vincent178/10889334">here</a> for a cheat-sheet of commands.</p>
</div>
</div>
<divclass="section"id="postgresql">
<h2>PostgreSQL<aclass="headerlink"href="#postgresql"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference external"href="https://www.postgresql.org/">PostgreSQL</a> is an open-source database engine, recommended by Django. While not as fast as SQLite for normal usage, it will scale better than SQLite, especially if your game has an very large database and/or extensive web presence through a separate server process.</p>
<h3>Install and initial setup of PostgreSQL<aclass="headerlink"href="#install-and-initial-setup-of-postgresql"title="Permalink to this headline">¶</a></h3>
<p>First, install the posgresql server. Version <codeclass="docutils literal notranslate"><spanclass="pre">9.6</span></code> is tested with Evennia. Packages are readily available for all distributions. You need to also get the <codeclass="docutils literal notranslate"><spanclass="pre">psql</span></code> client (this is called <codeclass="docutils literal notranslate"><spanclass="pre">postgresql-client</span></code> on debian-derived systems). Windows/Mac users can <aclass="reference external"href="https://www.postgresql.org/download/">find what they need on the postgresql download page</a>. You should be setting up a password for your database-superuser (always called <codeclass="docutils literal notranslate"><spanclass="pre">postgres</span></code>) when you install.</p>
<p>For interaction with Evennia you need to also install <codeclass="docutils literal notranslate"><spanclass="pre">psycopg2</span></code> to your Evennia install (<codeclass="docutils literal notranslate"><spanclass="pre">pip</span><spanclass="pre">install</span><spanclass="pre">psycopg2-binary</span></code> in your virtualenv). This acts as the python bridge to the database server.</p>
<div><p>:warning: <strong>Warning:</strong> With the <codeclass="docutils literal notranslate"><spanclass="pre">--password</span></code> argument, Postgres should prompt you for a password.
If it won’t, replace that with <codeclass="docutils literal notranslate"><spanclass="pre">-p</span><spanclass="pre">yourpassword</span></code> instead. Do not use the <codeclass="docutils literal notranslate"><spanclass="pre">-p</span></code> argument unless you have to since the resulting command, and your password, will be logged in the shell history.</p>
</div></blockquote>
<p>This will open a console to the postgres service using the psql client.</p>
<spanclass="c1">-- \l (list all databases and permissions)</span>
<spanclass="c1">-- \q (exit)</span>
</pre></div>
</td></tr></table></div>
<p><aclass="reference external"href="https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546">Here</a> is a cheat-sheet for psql commands.</p>
<p>We create a database user ‘evennia’ and a new database named <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span></code> (you can call them whatever you want though). We then grant the ‘evennia’ user full privileges to the new database so it can read/write etc to it.
If you in the future wanted to completely wipe the database, an easy way to do is to log in as the <codeclass="docutils literal notranslate"><spanclass="pre">postgres</span></code> superuser again, then do <codeclass="docutils literal notranslate"><spanclass="pre">DROP</span><spanclass="pre">DATABASE</span><spanclass="pre">evennia;</span></code>, then <codeclass="docutils literal notranslate"><spanclass="pre">CREATE</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">GRANT</span></code> steps above again to recreate the database and grant privileges.</p>
<p>as a shortcut to get into the postgres command line for the right database and user.</p>
<p>With the database setup you should now be able to start start Evennia normally with your new database.</p>
</div>
</div>
<divclass="section"id="mysql-mariadb">
<h2>MySQL / MariaDB<aclass="headerlink"href="#mysql-mariadb"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference external"href="https://www.mysql.com/">MySQL</a> is a commonly used proprietary database system, on par with PostgreSQL. There is an open-source alternative called <aclass="reference external"href="https://mariadb.org/">MariaDB</a> that mimics all functionality and command syntax of the former. So this section covers both.</p>
<h3>Installing and initial setup of MySQL/MariaDB<aclass="headerlink"href="#installing-and-initial-setup-of-mysql-mariadb"title="Permalink to this headline">¶</a></h3>
<p>First, install and setup MariaDB or MySQL for your specific server. Linux users should look for the <codeclass="docutils literal notranslate"><spanclass="pre">mysql-server</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">mariadb-server</span></code> packages for their respective distributions. Windows/Mac users will find what they need from the <aclass="reference external"href="https://www.mysql.com/downloads/">MySQL downloads</a> or <aclass="reference external"href="https://mariadb.org/download/">MariaDB downloads</a> pages. You also need the respective database clients (<codeclass="docutils literal notranslate"><spanclass="pre">mysql</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">mariadb-client</span></code>), so you can setup the database itself. When you install the server you should usually be asked to set up the database root user and password.</p>
<p>You will finally also need a Python interface to allow Evennia to talk to the database. Django recommends the <codeclass="docutils literal notranslate"><spanclass="pre">mysqlclient</span></code> one. Install this into the evennia virtualenv with <codeclass="docutils literal notranslate"><spanclass="pre">pip</span><spanclass="pre">install</span><spanclass="pre">mysqlclient</span></code>.</p>
<p>Start the database client (this is named the same for both mysql and mariadb):</p>
<spanclass="k">ALTER</span><spanclass="k">DATABASE</span><spanclass="o">`</span><spanclass="n">evennia</span><spanclass="o">`</span><spanclass="nb">CHARACTER</span><spanclass="k">SET</span><spanclass="n">utf8</span><spanclass="p">;</span><spanclass="c1">-- note that it's `evennia` with back-ticks, not quotes!</span>
<spanclass="c1">-- use 'exit' to quit client</span>
</pre></div>
</td></tr></table></div>
<p><aclass="reference external"href="https://gist.github.com/hofmannsven/9164408">Here</a> is a mysql command cheat sheet.</p>
<p>Above we created a new local user and database (we called both ‘evennia’ here, you can name them what you prefer). We set the character set to <codeclass="docutils literal notranslate"><spanclass="pre">utf8</span></code> to avoid an issue with prefix character length that can pop up on some installs otherwise. Next we grant the ‘evennia’ user all privileges on the <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span></code> database and make sure the privileges are applied. Exiting the client brings us back to the normal terminal/console.</p>
<blockquote>
<div><p>Note: If you are not using MySQL for anything else you might consider granting the ‘evennia’ user full privileges with <codeclass="docutils literal notranslate"><spanclass="pre">GRANT</span><spanclass="pre">ALL</span><spanclass="pre">PRIVILEGES</span><spanclass="pre">ON</span><spanclass="pre">*.*</span><spanclass="pre">TO</span><spanclass="pre">'evennia'@'localhost';</span></code>. If you do, it means you can use <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span><spanclass="pre">dbshell</span></code> later to connect to mysql, drop your database and re-create it as a way of easy reset. Without this extra privilege you will be able to drop the database but not re-create it without first switching to the database-root user.</p>
<h2>Add MySQL configuration to Evennia<aclass="headerlink"href="#add-mysql-configuration-to-evennia"title="Permalink to this headline">¶</a></h2>
<p>To tell Evennia to use your new database you need to edit <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf/settings.py</span></code> (or <codeclass="docutils literal notranslate"><spanclass="pre">secret_settings.py</span></code> if you don’t want your db info passed around on git repositories).</p>
<blockquote>
<div><p>Note: The Django documentation suggests using an external <codeclass="docutils literal notranslate"><spanclass="pre">db.cnf</span></code> or other external conf-formatted file. Evennia users have however found that this leads to problems (see e.g. <aclass="reference external"href="https://git.io/vQdiN">issue #1184</a>). To avoid trouble we recommend you simply put the configuration in your settings as below.</p>
<spanclass="s1">'HOST'</span><spanclass="p">:</span><spanclass="s1">'localhost'</span><spanclass="p">,</span><spanclass="c1"># or an IP Address that your DB is hosted on</span>
<spanclass="s1">'PORT'</span><spanclass="p">:</span><spanclass="s1">''</span><spanclass="p">,</span><spanclass="c1"># use default port</span>
<spanclass="p">}</span>
<spanclass="p">}</span>
</pre></div>
</td></tr></table></div>
<p>Change this to fit your database setup. Next, run:</p>
<p>as a shortcut to get into the postgres command line for the right database and user.</p>
<p>With the database setup you should now be able to start start Evennia normally with your new database.</p>
</div>
<divclass="section"id="others">
<h2>Others<aclass="headerlink"href="#others"title="Permalink to this headline">¶</a></h2>
<p>No testing has been performed with Oracle, but it is also supported through Django. There are community maintained drivers for <aclass="reference external"href="http://code.google.com/p/django-mssql/">MS SQL</a> and possibly a few others. If you try other databases out, consider expanding this page with instructions.</p>