场景
记录ubantu20.04安装mysql5.7
一、安装
# 更新系统软件包
sudo spt update
# 安装mysql
sudo apt mysql-server-5.7 mysql-client-5.7
看到下面这句话,重启一下服务器
ureadahead will be reprofiled on next reboot
重启后直接查看状态
sudo systemctl status mysql
看到active就是启动成功
Active: active (running)
二、设置
安全相关
sudo mysql_secure_installation
出现提示,需要配置一下
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
# 有个密码强度的插件,在设置密码的时候会校验密码强度,是否启用
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
# 如果上一步选择了校验密码强度,这里需要选择校验强度的等级
Please set the password for root here.
New password:
# 输入新密码,密码需要符合上面所选的等级要求
Re-enter new password:
# 确认密码
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
# 是否要通过刚设置的密码连接mysql
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
# 是否移除匿名用户
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
# 不允许root用户远程登录?
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
# 是否移除test库
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
# 刷新权限表
三、连接
1.本地命令连接
sudo mysql -u root -p
2.远程连接
- 启用mysql远程连接,修改mysql配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释bind-address = 127.0.0.1
修改完成之后重启数据库服务
sudo systemctl restart mysql
- 创建远程登录账号
使用root本地登录mysql
-- 创建zhangsan账号
create user 'zhangsan'@'%' identified by 'zhangsan@1234';
-- 授予数据库权限(所有)
grant all privileges on *.* to 'zhangsan'@'%' with grant option;
-- 刷新权限
flush privileges;
远程使用zhangsan/zhangsan@1234就可以登录了
四、总结
数据库往往都是一个项目或者是一个任务的开始,希望大家后面遇到问题都能不懈应对,保持热爱
最后说一句:数据库密码还是应该设置复杂一点,不然存在数据被盗风险(有幸历过数据被盗),root只允许本地登录,账号权限也应该细化。