安装mysql

apt -y install mysql-server mysql-client
  • 1.

删除空用户登录信息

mysql
USE mysql;
delete from user where user = "";
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
exit;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

初始化数据库

sudo mysql_secure_installation
  • 1.

y→设置root密码→y→是否禁止root远程登录,更具需求选择y/n,建议禁止→y→y设置远程

 

alter user 'root'@'localhost' identified by 'password@123';
create user 'database1'@'localhost' identified by 'password@123';
grant all privileges on *.* to 'database1'@'%' with grant option;
FLUSH PRIVILEGES;
  • 1.
  • 2.
  • 3.
  • 4.

创建只读账号

CREATE USER 'readonly_user'@'%' IDENTIFIED BY 'password';
GRANT SELECT ON *.* TO 'readonly_user'@'%';
FLUSH PRIVILEGES;
  • 1.
  • 2.
  • 3.