5.子系统WSL ubuntu18.04安装mysql

更新库
sudo apt-get update
安装 MySql
sudo apt-get install mysql-server
初始化配置
sudo mysql_secure_installation
配置项较多,如下所示:
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

配置远程访问
在Ubuntu下MySQL缺省是只允许本地访问
如果你要其他机器也能够访问的话,需要进行配置;
配置mysql文件
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
把bind-address注释掉才能远程访问
#bind-address = 127.0.0.1
# 下面这个可不理会
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 重启mysql
sudo service mysql restart
# 查看路由3306端口
sudo iptables -L -n | grep 3306
# 没有就添加临时规则
sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# 或者永久
sudo ufw allow 3306/tcp

用根用户进入
sudo mysql -uroot -p
登入root进行其他设置:
GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "密码";
其中root@localhos,localhost就是本地访问,配置成%就是所有主机都可连接。

新建数据库和用户
用root用户新建数据和用作远程访问的用户
##1 创建数据库mydata
CREATE DATABASE "mydata";
--新建用户ss33,和登陆密码,设置访问限制为%,允许远程访问。
mysql>CREATE USER 'ss33'@'%' IDENTIFIED BY '密码';
并允许ss33用户可以从任意机器上登入mysql的mydata数据库
GRANT ALL PRIVILEGES ON mydata.* TO ss33@"%" IDENTIFIED BY "密码";
或 所有数据库所有权限 用这个方便
GRANT ALL PRIVILEGES ON *.* TO 'ss33'@'%' IDENTIFIED BY '密码';

--刷新权限列表,这样就可以用ss33用户登陆了
mysql> flush privileges;
mysql> exit

点赞