查看有没有安装MySQL:
dpkg -l | grep mysql
安装MySQL:
apt install mysql-server
apt install mysql-client
apt install libmysqlclient-dev
安装完成之后可以使用如下命令来检查是否安装成功:
netstat -tap | grep mysql
mysql_secure_installation
该脚本将通过一系列的提示帮你完成MySQL安装安全选项的变更。第一个提示将询问你是否愿意安装密码检测插件,该插件用来测试你设置的MySQL密码的强壮性。无论你如何选择,下一个提示是让你设置MySQL root用户的密码。回车,然后需要确认你输入的密码。
secure enough. Would you like to setup VALIDATE PASSWORD plugin? # 要安装验证密码插件吗?
Press y|Y for Yes, any other key for No: N # 这里我选择N
Please set the password for root here.
New password: # 输入要为root管理员设置的数据库密码
Re-enter new password: # 再次输入密码
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. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 删除匿名账户
Success.
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 # 禁止root管理员从远程登录,这里我没有禁止
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y # 删除test数据库并取消对它的访问权限
Dropping test database...
Success.Removing privileges on test database...
Success.
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 # 刷新授权表,让初始化后的设定立即生效
Success.
All done!
现在配置mysql允许远程访问,首先编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 配置文件:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释掉bind-address = 127.0.0.1
用户创建参考
https://www.cnblogs.com/wuxunyan/p/9095016.html
新建用户
格式:create user "username"@"host" identified by "password";
授权
格式:grant privileges on databasename.tablename to 'username'@'host' IDENTIFIED BY 'PASSWORD';
1. GRANT命令说明:
priveleges(权限列表),可以是all priveleges, 表示所有权限,也可以是select、update等权限,多个权限的名词,相互之间用逗号分开。
on用来指定权限针对哪些库和表。
. 中前面的号用来指定数据库名,后面的号用来指定表名。
to 表示将权限赋予某个用户, 如 jack@'localhost' 表示jack用户,@后面接限制的主机,可以是IP、IP段、域名以及%,%表示任何地方。注意:这里%有的版本不包括本地,以前碰到过给某个用户设置了%允许任何地方登录,但是 在本地登录不了,这个和版本有关系,遇到这个问题再加一个localhost的用户就可以了。
identified by指定用户的登录密码,该项可以省略。
WITH GRANT OPTION 这个选项表示该用户可以将自己拥有的权限授权给别人。注意:经常有人在创建操作用户的时候不指定WITH GRANT OPTION选项导致后来该用户不能使用GRANT命令创建用户或者给其它用户授权。 备注:可以使用GRANT重复给用户添加权限,权限叠加,比如你先给用户添加一个select权限,然后又给用户添加一个insert权限,那么该用户就同时拥有了select和insert权限。
2.授权原则说明:
权限控制主要是出于安全因素,因此需要遵循一下几个经验原则:
a、只授予能满足需要的最小权限,防止用户干坏事。比如用户只是需要查询,那就只给select权限就可以了,不要给用户赋予update、insert或者delete权限。
b、创建用户的时候限制用户的登录主机,一般是限制成指定IP或者内网IP段。 c、初始化数据库的时候删除没有密码的用户。安装完数据库的时候会自动创建一些用户,这些用户默认没有密码。 d、为每个用户设置满足密码复杂度的密码。
e、定期清理不需要的用户。回收权限或者删除用户。
eg:
/授予用户通过外网IP对于该数据库的全部权限/
grant all privileges on test
.* to 'test'@'%' ;
/授予用户在本地服务器对该数据库的全部权限/
grant all privileges on test
.* to 'test'@'localhost';
grant select on test.* to 'user1'@'localhost'; /给予查询权限/
grant insert on test.* to 'user1'@'localhost'; /添加插入权限/
grant delete on test.* to 'user1'@'localhost'; /添加删除权限/
grant update on test.* to 'user1'@'localhost'; /添加权限/
flush privileges; /刷新权限/