Install MySQL at WSL

From WikiMLT
Revision as of 10:47, 12 February 2023 by Spas (talk | contribs) (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:Databases)

In­stall mysql-serv­er and mysql-client.

sudo apt install -y mysql-client mysql-server
sudo service mysql start
sudo service mysql status
mysql --version

Then cre­ate a MySQL user that is able to log-in in­to the MySQL serv­er by us­ing na­tive au­then­ti­ca­tion. Gran to the user all priv­i­leges.

sudo mysql
CREATE USER 'local_admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'local_admin_password_1!2@3#';
GRANT ALL PRIVILEGES ON *.* TO 'local_admin'@'localhost' WITH GRANT OPTION;
SELECT user,plugin,host FROM mysql.user WHERE host = 'localhost';
+------------------+-----------------------+-----------+
| user             | plugin                | host      |
+------------------+-----------------------+-----------+
| debian-sys-maint | caching_sha2_password | localhost |
| local_admin      | mysql_native_password | localhost |
| mysql.infoschema | caching_sha2_password | localhost |
| mysql.session    | caching_sha2_password | localhost |
| mysql.sys        | caching_sha2_password | localhost |
| root             | auth_socket           | localhost |
+------------------+-----------------------+-----------+
6 rows in set (0.00 sec)
quit

Now log-in by that user in or­der to test it and cre­ate one new emp­ty da­ta base for the fur­ther tests.

mysql -u'local_admin' -p'local_admin_password_1!2@3#'
CREATE DATABASE nodejs_w3s_tests;
Query OK, 1 row affected (0.01 sec)
quit

In or­der to start the MySQL serv­er au­to­mat­i­cal­ly at user log-in, cre­ate or up­date the file wsl-services.bat, lo­cat­ed in the Win­dows user's fold­er shell:startup, with the fol­low­ing en­try.

wsl-services.bat
C:\Windows\System32\wsl.exe -u root /etc/init.d/mysql start

Ref­er­ences: