Ubuntu安装MySQL
sudo apt update
sudo apt install mysql-server
sudo systemctl status mysql
报错:
root@LAPTOP-2CLP2VHG:~# sudo systemctl status mysql
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
继续安装
sudo apt install systemctl
执行结果:
root@xxx:/home/xxx# sudo systemctl status mysql
mysql.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service, enabled)
Active: inactive (dead)
root@xxx:/home/xxx# sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT 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 component?
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:
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) : y
Success.
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!
两种开启mysql服务方法
root@xxx:/home/xxx# sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.28-0ubuntu0.20.04.4 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit;
Bye
root@xxx:/home/xxx# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.28-0ubuntu0.20.04.4 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit;
Bye
想设置权限,一直报错,初始权限是如图
select user, plugin from mysql.user;
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'mysql123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
//把密码等级设为0
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
//密码长度设为6位长
mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)
//然后用mysql_native_password 权限登录密码设为六位长的
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'xxxxxx';
Query OK, 0 rows affected (0.01 sec)
这样设置完只能通过’mysql -uroot -p’密码登录了
后端代码连接也不会报错了failed to initialize database, got error Error 1698: Access denied for u ser ‘root’@‘localhost’,Navicat也可以连上MySQL数据库。
MySQL查看用户权限
mysql> select user, plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| debian-sys-maint | caching_sha2_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session | caching_sha2_password |
| mysql.sys | caching_sha2_password |
| root | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)