ubuntu安装mysql

卸载

删除mysql的数据文件

sudo rm /var/lib/mysql/ -R

 
删除mysql的配置文件

sudo rm /etc/mysql/ -R

 
自动卸载mysql(包括server和client)

sudo apt-get autoremove mysql* --purge

sudo apt-get remove apparmor

 
然后在终端中查看MySQL的依赖项:dpkg --list|grep mysql

安装

sudo apt update
sudo apt install mysql-server

安装完成后,MySQL服务将自动启动。要验证MySQL服务器正在运行,请输入:

sudo systemctl status mysql

 

 

 安全操作

输入root的密码,然后一系列的配置

root@VM-0-2-ubuntu:/etc/mysql# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: root

Invalid option provided.

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: 12345678
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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

 ... 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
 - 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!

登录

sudo mysql
或者
mysql -u root -p

 

 

 创建用户

命令:
CREATE USER ‘username’@’host’ IDENTIFIED BY ‘password’;
说明:
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
例子:
CREATE USER ‘dog’@’localhost’ IDENTIFIED BY ‘123456’;
CREATE USER ‘pig’@’192.168.1.101_’ IDENDIFIED BY ‘123456’;
CREATE USER ‘pig’@’%’ IDENTIFIED BY ‘123456’;
CREATE USER ‘pig’@’%’ IDENTIFIED BY ”;
CREATE USER ‘pig’@’%’;

但是报错,因为上述代码的引号是中文字符,抄作业的后果

 

 

 

// 成功代码
CREATE USER 'dimp'@'%' IDENTIFIED BY '12345678';

查看用户

SELECT User,Host FROM mysql.user;

 

为用户授权

//授予userone全部数据库权限,并修改密码
grant all on *.* to 'dimp'@'%' identified by '12345678';

 

其他命令

// 查看密码策略
SHOW VARIABLES LIKE 'validate_password%'
// 设置密码强度为low
set global validate_password_policy=LOW;

 

关于 mysql 密码策略相关参数;
1)validate_password_length  固定密码的总长度;
2)validate_password_dictionary_file 指定密码验证的文件路径;
3)validate_password_mixed_case_count  整个密码中至少要包含大/小写字母的总个数;
4)validate_password_number_count  整个密码中至少要包含阿拉伯数字的个数;
5)validate_password_policy 指定密码的强度验证等级,默认为 MEDIUM;
关于 validate_password_policy 的取值:
0/LOW:只验证长度;
1/MEDIUM:验证长度、数字、大小写、特殊字符;
2/STRONG:验证长度、数字、大小写、特殊字符、字典文件;
6)validate_password_special_char_count 整个密码中至少要包含特殊字符的个数

 

 

 

查看端口

show global variables like 'port';

 

报错:sh: 0: getcwd() failed: No such file or directory

在执行命令前加 cd ~

 

posted @ 2021-06-24 23:34  小鸡蛋白  阅读(11140)  评论(0编辑  收藏  举报