|
| 1 | +--- |
| 2 | +title: Get started using MySQL MongoDB, PostgreSQL, SQLite, Microsoft SQL Server, or Redis to set up a database on Windows Subsystem for Linux |
| 3 | +description: Learn how to set up MySQL MongoDB, PostgreSQL, SQLite, Microsoft SQL Server, or Redis on the Windows Subsystem for Linux. |
| 4 | +keywords: wsl, windows, windowssubsystem, MySQL MongoDB, PostgreSQL, SQLite, Microsoft SQL Server, Redis, windows 10 |
| 5 | +ms.date: 07/07/2020 |
| 6 | +ms.topic: article |
| 7 | +ms.localizationpriority: medium |
| 8 | +--- |
| 9 | + |
| 10 | +# Get started with databases on Windows Subsystem for Linux |
| 11 | + |
| 12 | +This step-by-step guide will help you get started connecting your project in WSL to a database. Get started with MySQL, PostgreSQL, MongoDB, Redis, Microsoft SQL Server, or SQLite. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Running Windows 10, [updated to version 2004](ms-settings:windowsupdate), **Build 19041** or higher. |
| 17 | +- [WSL enabled and installed, and updated to WSL 2](https://docs.microsoft.com/windows/wsl/install-win10). |
| 18 | +- [Linux distribution installed](https://docs.microsoft.com/windows/wsl/install-win10#install-your-linux-distribution-of-choice) (Ubuntu 18.04 for our examples). |
| 19 | +- Ensure your Ubuntu 18.04 distribution is [running in WSL 2 mode](https://docs.microsoft.com/windows/wsl/install-win10#set-your-distribution-version-to-wsl-1-or-wsl-2). (WSL can run distributions in both v1 or v2 mode.) You can check this by opening PowerShell and entering: `wsl -l -v` |
| 20 | + |
| 21 | +## Differences between database systems |
| 22 | + |
| 23 | +The most [popular choices](https://insights.stackoverflow.com/survey/2019#technology-_-databases) for a database system include: |
| 24 | + |
| 25 | +- [MySQL](https://www.mysql.com/why-mysql/) (SQL) |
| 26 | +- [PostgreSQL](https://www.postgresql.org/about/) (SQL) |
| 27 | +- [Microsoft SQL Server](https://docs.microsoft.com/sql/?view=sql-server-ver15) (SQL) |
| 28 | +- [SQLite](https://www.sqlite.org/about.html) (SQL) |
| 29 | +- [MongoDB](https://www.mongodb.com/what-is-mongodb) (NoSQL) |
| 30 | +- [Redis](https://redis.io/topics/introduction) (NoSQL) |
| 31 | + |
| 32 | +**MySQL** is an open-source SQL relational database, organizing data into one or more tables in which data types may be related to each other. It is vertically scalable, which means one ultimate machine will do the work for you. It is currently the most widely used of the four database systems. |
| 33 | + |
| 34 | +**PostgreSQL** (sometimes referred to as Postgres) is also an open-source SQL relational database with an emphasis on extensibility and standards compliance. It can handle JSON now too, but it is generally better for structured data, vertical scaling, and ACID-compliant needs like eCommerce and financial transactions. |
| 35 | + |
| 36 | +**Microsoft SQL Server** includes SQL Server on Windows, SQL Server on Linux, and SQL on Azure. These are also relational database management systems set up on servers with primary function of storing and retrieving data as requested by software applications. |
| 37 | + |
| 38 | +**SQLite** is an open-source self-contained, file-based, “serverless” database, known for its portability, reliability, and good performance even in low-memory environments. |
| 39 | + |
| 40 | +**MongoDB** is an open-source NoSQL document database designed to work with JSON and store schema-free data. It is horizontally scalable, which means multiple smaller machines will do the work for you. It's good for flexibility and unstructured data, and caching real-time analytics. |
| 41 | + |
| 42 | +**Redis** is is an open-source NoSQL in-memory data structure store. It uses key-value pairs for storage instead of documents. Redis is known for its flexibility, performance, and wide language support. It’s flexible enough to be used as a cache or message broker and can use data structures like lists, sets, and hashes. |
| 43 | + |
| 44 | +The sort of database you choose should depend on the type of application you will be using the database with. We recommend that you look up the advantages and disadvantages of structured and unstructured databases and choose based on your use case. |
| 45 | + |
| 46 | +## Install MySQL |
| 47 | + |
| 48 | +To install MySQL on WSL (Ubuntu 18.04): |
| 49 | + |
| 50 | +1. Open your WSL terminal (ie. Ubuntu 18.04). |
| 51 | +2. Update your Ubuntu packages: `sudo apt update` |
| 52 | +3. Once the packages have updated, install MySQL with: `sudo apt install mysql-server` |
| 53 | +4. Confirm installation and get the version number: `mysql --version` |
| 54 | + |
| 55 | +You may also want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users. To run the security script: |
| 56 | + |
| 57 | +1. Start a MySQL server: `sudo /etc/init.d/mysql start` |
| 58 | +2. Start the security script prompts: `sudo mysql_secure_installation` |
| 59 | +3. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which can be used to test the strength of your MySQL password. You will then set a password for the MySQL root user, decide whether or not to remove anonymous users, decide whether to allow the root user to login both locally and remotely, decide whether to remove the test database, and, lastly, decide whether to reload the privilege tables immediately. |
| 60 | + |
| 61 | +To open the MySQL prompt, enter: `sudo mysql` |
| 62 | + |
| 63 | +To see what databases you have available, in the MySQL prompt, enter: `SHOW DATABASES;` |
| 64 | + |
| 65 | +To create a new database, enter: `CREATE DATABASE database_name;` |
| 66 | + |
| 67 | +To delete a database, enter: ` DROP DATABASE database_name;` |
| 68 | + |
| 69 | +For more about working with MySQL databases, see the [MySQL docs](https://dev.mysql.com/doc/mysql-getting-started/en/). |
| 70 | + |
| 71 | +To work with with MySQL databases in VS Code, try the [MySQL extension](https://marketplace.visualstudio.com/items?itemName=cweijan.vscode-mysql-client2). |
| 72 | + |
| 73 | +## Install PostgreSQL |
| 74 | + |
| 75 | +To install PostgreSQL on WSL (Ubuntu 18.04): |
| 76 | + |
| 77 | +1. Open your WSL terminal (ie. Ubuntu 18.04). |
| 78 | +2. Update your Ubuntu packages: `sudo apt update` |
| 79 | +3. Once the packages have updated, install PostgreSQL (and the -contrib package which has some helpful utilities) with: `sudo apt install postgresql postgresql-contrib` |
| 80 | +4. Confirm installation and get the version number: `psql --version` |
| 81 | + |
| 82 | +There are 3 commands you need to know once PostgreSQL is installed: |
| 83 | + |
| 84 | +- `sudo service postgresql status` for checking the status of your database. |
| 85 | +- `sudo service postgresql start` to start running your database. |
| 86 | +- `sudo service postgresql stop` to stop running your database. |
| 87 | + |
| 88 | +The default admin user, `postgres`, needs a password assigned in order to connect to a database. To set a password: |
| 89 | + |
| 90 | +1. Enter the command: `sudo passwd postgres` |
| 91 | +2. You will get a prompt to enter your new password. |
| 92 | +3. Close and reopen your terminal. |
| 93 | + |
| 94 | +To run PostgreSQL with [psql](https://www.postgresql.org/docs/10/app-psql.html) shell: |
| 95 | + |
| 96 | +1. Start your postgres service: `sudo service postgresql start` |
| 97 | +2. Connect to the postgres service and open the psql shell: `sudo -u postgres psql` |
| 98 | + |
| 99 | +Once you have successfully entered the psql shell, you will see your command line change to look like this: `postgres=#` |
| 100 | + |
| 101 | +> [!NOTE] |
| 102 | +> Alternatively, you can open the psql shell by switching to the postgres user with: `su - postgres` and then entering the command: `psql`. |
| 103 | +
|
| 104 | +To exit postgres=# enter: `\q` or use the shortcut key: Ctrl+D |
| 105 | + |
| 106 | +To see what user accounts have been created on your PostgreSQL installation, use from your WSL terminal: `psql -c "\du"` ...or just `\du` if you have the psql shell open. This command will display columns: Account User Name, List of Roles Attributes, and Member of role group(s). To exit back to the command line, enter: `q`. |
| 107 | + |
| 108 | +For more about working with PostgreSQL databases, see the [PostgreSQL docs](https://www.postgresql.org/docs/13/tutorial-createdb.html). |
| 109 | + |
| 110 | +To work with with PostgreSQL databases in VS Code, try the [PostgreSQL extension](https://marketplace.visualstudio.com/items?itemName=ms-ossdata.vscode-postgresql). |
| 111 | + |
| 112 | +## Install MongoDB |
| 113 | + |
| 114 | +To install MongoDB on WSL (Ubuntu 18.04): |
| 115 | + |
| 116 | +1. Open your WSL terminal (ie. Ubuntu 18.04). |
| 117 | +2. Update your Ubuntu packages: `sudo apt update` |
| 118 | +3. Once the packages have updated, install MongoDB with: `sudo apt-get install mongodb` |
| 119 | +4. Confirm installation and get the version number: `mongod --version` |
| 120 | + |
| 121 | +There are 3 commands you need to know once MongoDB is installed: |
| 122 | + |
| 123 | +- `sudo service mongodb status` for checking the status of your database. |
| 124 | +- `sudo service mongodb start` to start running your database. |
| 125 | +- `sudo service mongodb stop` to stop running your database. |
| 126 | + |
| 127 | +> [!NOTE] |
| 128 | +> You might see the command `sudo systemctl status mongodb` used in tutorials or articles. In order to remain lightweight, WSL does not include `systemd` (a service management system in Linux). Instead, it uses SysVinit to start services on your machine. You shouldn't notice a difference, but if a tutorial recommends using `sudo systemctl`, instead use: `sudo /etc/init.d/`. For example, `sudo systemctl status mongodb`, for WSL would be `sudo /etc/inid.d/mongodb status` ...or you can also use `sudo service mongodb status`. |
| 129 | +
|
| 130 | +To run your Mongo database in a local server: |
| 131 | + |
| 132 | +1. Check the status of your database: `sudo service mongodb status` |
| 133 | + You should see a [Fail] response, unless you've already started your database. |
| 134 | + |
| 135 | +2. Start your database: `sudo service mongodb start` |
| 136 | + You should now see an [OK] response. |
| 137 | + |
| 138 | +3. Verify by connecting to the database server and running a diagnostic command: `mongo --eval 'db.runCommand({ connectionStatus: 1 })'` |
| 139 | + This will output the current database version, the server address and port, and the output of the status command. A value of `1` for the "ok" field in the response indicates that the server is working. |
| 140 | + |
| 141 | +4. To stop your MongoDB service from running, enter: `sudo service mongodb stop` |
| 142 | + |
| 143 | +> [!NOTE] |
| 144 | +> MongoDB has several default parameters, including storing data in /data/db and running on port 27017. Also, `mongod` is the daemon (host process for the database) and `mongo` is the command-line shell that connects to a specific instance of `mongod`. |
| 145 | +
|
| 146 | +VS Code supports working with MongoDB databases via the [Azure CosmosDB extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-cosmosdb), you can create, manage and query MongoDB databases from within VS Code. To learn more, visit the VS Code docs: [Working with MongoDB](https://code.visualstudio.com/docs/azure/mongodb). |
| 147 | + |
| 148 | +Learn more in the MongoDB docs: |
| 149 | + |
| 150 | +- [Introduction to using MongoDB](https://docs.mongodb.com/manual/introduction/) |
| 151 | +- [Create users](https://docs.mongodb.com/manual/tutorial/create-users/) |
| 152 | +- [Connect to a MongoDB instance on a remote host](https://docs.mongodb.com/manual/mongo/#mongodb-instance-on-a-remote-host) |
| 153 | +- [CRUD: Create, Read, Update, Delete](https://docs.mongodb.com/manual/crud/) |
| 154 | +- [Reference Docs](https://docs.mongodb.com/manual/reference/) |
| 155 | + |
| 156 | +## Install Microsoft SQL Server |
| 157 | + |
| 158 | +To install SQL Server on WSL (Ubuntu 18.04), follow this quickstart: [Install SQL Server and create a database on Ubuntu](https://docs.microsoft.com/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver15). |
| 159 | + |
| 160 | +To work with Microsoft SQL Server databases in VS Code, try the [MSSQL extension](https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql). |
| 161 | + |
| 162 | +## Install SQLite |
| 163 | + |
| 164 | +To install SQLite on WSL (Ubuntu 18.04): |
| 165 | + |
| 166 | +1. Open your WSL terminal (ie. Ubuntu 18.04). |
| 167 | +2. Update your Ubuntu packages: `sudo apt update` |
| 168 | +3. Once the packages have updated, install SQLite3 with: `sudo apt install sqlite3` |
| 169 | +4. Confirm installation and get the version number: `sqlite3 --version` |
| 170 | + |
| 171 | +To create a test database, called "example.db", enter: `sqlite3 example.db` |
| 172 | + |
| 173 | +To see a list of your SQLite databases, enter: `.databases` |
| 174 | + |
| 175 | +To see the status of your database, enter: `.dbinfo ?DB?` |
| 176 | + |
| 177 | +To exit the SQLite prompt, enter: `.exit` |
| 178 | + |
| 179 | +For more information about working with a SQLite database, see the [SQLite docs](https://www.sqlite.org/quickstart.html). |
| 180 | + |
| 181 | +To work with SQLite databases in VS Code, try the [SQLite extension](https://marketplace.visualstudio.com/items?itemName=mtxr.sqltools). |
| 182 | + |
| 183 | +## Install Redis |
| 184 | + |
| 185 | +To install Redis on WSL (Ubuntu 18.04): |
| 186 | + |
| 187 | +1. Open your WSL terminal (ie. Ubuntu 18.04). |
| 188 | +2. Update your Ubuntu packages: `sudo apt update` |
| 189 | +3. Once the packages have updated, install Redis with: `sudo apt install redis-server` |
| 190 | +4. Confirm installation and get the version number: `redis-server --version` |
| 191 | + |
| 192 | +To start running your Redis server: `sudo service redis-server start` |
| 193 | + |
| 194 | +Check to see if redis is working (redis-cli is the command line interface utility to talk with Redis): `redis-cli ping` this should return a reply of "PONG". |
| 195 | + |
| 196 | +To stop running your Redis server: `sudo service redis-server stop` |
| 197 | + |
| 198 | +For more information about working with a Redis database, see the [Redis docs](https://redis.io/topics/quickstart). |
| 199 | + |
| 200 | +To work with Redis databases in VS Code, try the [Redis extension](https://marketplace.visualstudio.com/items?itemName=cweijan.vscode-redis-client). |
| 201 | + |
| 202 | +## See services running and set up profile aliases |
| 203 | + |
| 204 | +To see the services that you currently have running on your WSL distribution, enter: `service --status-all` |
| 205 | + |
| 206 | +Typing out `sudo service mongodb start` or `sudo service postgres start` and `sudo -u postgrest psql` can get tedious. However, you could consider setting up aliases in your `.profile` file on WSL to make these commands quicker to use and easier to remember. |
| 207 | + |
| 208 | +To set up your own custom alias, or shortcut, for executing these commands: |
| 209 | + |
| 210 | +1. Open your WSL terminal and enter `cd ~` to be sure you're in the root directory. |
| 211 | +2. Open the `.profile` file, which controls the settings for your terminal, with the terminal text editor, Nano: `sudo nano .profile` |
| 212 | +3. At the bottom of the file (don't change the `# set PATH` settings), add the following: |
| 213 | + |
| 214 | + ```bash |
| 215 | + # My Aliases |
| 216 | + alias start-pg='sudo service postgresql start' |
| 217 | + alias run-pg='sudo -u postgres psql' |
| 218 | + ``` |
| 219 | + |
| 220 | + This will allow you to enter `start-pg` to start running the postgresql service and `run-pg` to open the psql shell. You can change `start-pg` and `run-pg` to whatever names you want, just be careful not to overwrite a command that postgres already uses! |
| 221 | + |
| 222 | +4. Once you've added your new aliases, exit the Nano text editor using **Ctrl+X** -- select `Y` (Yes) when prompted to save and Enter (leaving the file name as `.profile`). |
| 223 | +5. Close and re-open your WSL terminal, then try your new alias commands. |
| 224 | +
|
| 225 | +## Additional resources |
| 226 | +
|
| 227 | +- [Setting up your development environment on Windows 10](https://docs.microsoft.com/windows/dev-environment/) |
0 commit comments