【MySQL】Ubuntu22.04安装MySQL8数据库详解

00. 目录


在这里插入图片描述

01. 安装MySQL

1.1 更新软件源

deng@local:~/code/3linux/9mutex_cond$ sudo apt update

1.2 安装mysql-server

deng@local:~/code/3linux/9mutex_cond$ sudo apt install mysql-server

1.3 安装libmysqlclient-dev

deng@local:~/code/3linux/9mutex_cond$ sudo apt install libmysqlclient-dev

02. 配置MySQL

2.1 进入mysql

deng@local:~$ sudo mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2023, 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> 


2.2 执行以下命令

然后运行以下 ALTER USER 命令将 root 用户的身份验证方法更改为使用密码的方法。 以下示例将身份验证方法更改为 mysql_native_password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '12345678';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

2.3 退出MySQL

mysql> quit
Bye
deng@local:~$ 

2.4 以sudo权限运行mysql_secure_installation

deng@local:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

# 输入密码password
Enter password for user root: 


2.5 设置密码策略和密码

deng@local:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

# 输入密码password
Enter password for user root: 

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?

# 输入Y
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

# 输入0
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.

# 输入Y
Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 


# 输入密码12345678
New password: 

# 输入密码12345678
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.


2.6 其它设置

# 输入N
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

# 输入Y
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.


# 输入N
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

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

03. 查看MySQL状态

显示active表示已经在运行

deng@local:~/code/3linux/9mutex_cond$ sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Thu 2023-09-07 17:25:26 CST; 10min ago
    Process: 6711 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
   Main PID: 6719 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 4556)
     Memory: 366.7M
        CPU: 6.904s
     CGroup: /system.slice/mysql.service
             └─6719 /usr/sbin/mysqld

9月 07 17:25:26 local systemd[1]: Starting MySQL Community Server...
9月 07 17:25:26 local systemd[1]: Started MySQL Community Server.
lines 1-14/14 (END)


04. 登录MySQL数据库

执行以下命令

deng@local:~$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2023, 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> 

05. 问题讨论

06. 附录

参考: 【数据库】Ubuntu18.04安装MySQL详解

官网: MySQL官网

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/132745573