UbuntuServer22.04LTS在线安装MySQL8.x
文章目录
- UbuntuServer22.04LTS在线安装MySQL8.x
- [1. 安装](#1. 安装)
-
- [1. 官网](#1. 官网)
- [2. 在线安装](#2. 在线安装)
- [3. 修改密码及设置远程登录](#3. 修改密码及设置远程登录)
- [4. 其他配置参考](#4. 其他配置参考)
- [2. 启动和停止](#2. 启动和停止)
-
- [1. 查看运行状态](#1. 查看运行状态)
- [2. 开机自启](#2. 开机自启)
- [3. 查看默认服务器配置命令](#3. 查看默认服务器配置命令)
- [3. 登录](#3. 登录)
1. 安装
1. 官网
官网安装文档:MySQL :: MySQL 8.0 Reference Manual
官网APT在线安装文档:MySQL :: 使用 MySQL APT 存储库的快速指南
2. 在线安装
shell
# 1.更新源
sudo apt update
# 2. 升级(可选)
sudo apt upgrade
# 3.安装MySQL服务
sudo apt install mysql-server
# 4. 看到下面信息则说明安装成功
done!
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up libhtml-parser-perl (3.72-5) ...#########################################################################################......................]
Setting up libhttp-message-perl (6.22-1) ...###########################################################################################...................]
Setting up mysql-server-8.0 (8.0.31-0ubuntu0.20.04.1) ...#################################################################################................]
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode#########################################...............]
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 1631
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up libcgi-pm-perl (4.46-1) ...#######################################################################################################.............]
Setting up libhtml-template-perl (2.97-1) ...###################################################################################################..........]
Setting up mysql-server (8.0.31-0ubuntu0.20.04.1) ...#############################################################################################........]
Setting up libcgi-fast-perl (1:2.15-1) ...###########################################################################################################.....]
Processing triggers for systemd (245.4-4ubuntu3.18) ...#################################################################################################..]
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
root@jinshengyuan:/etc/mysql/conf.d#
# 5. 查看安装版本
root@jinshengyuan:~# mysql -V
mysql Ver 8.0.31-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
3. 修改密码及设置远程登录
安装完成后默认无密码
- 设置密码
这里以密码
123456
举例,最好设置为复杂的密码
shell
# 1.先设置密码
mysql> alter user 'root'@'localhost' IDENTIFIED BY '123456';
mysql> flush privileges;
# 2.设置远程访问
msyql> alter user 'root'@'%' identified with mysql_native_password by '123456';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 设置远程访问
shell
# 方式1:
mysql> GRANT ALL ON *.* TO 'root'@'%';
# 方式2:
mysql> use mysql
mysql> update user set host = '%' where user='root';
mysql> flush privileges;
- 查看认证插件
shell
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
4. 其他配置参考
/etc/mysql/my.cnf
my.cnf配置文件内容如下
ini
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
# basedir=/home/lab_soft/mysql-8.0.31
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
# datadir=/home/lab_soft/mysql-8.0.31 #8.0以下版本需要配置数据目录
# 允许最大连接数
max_connections=5000
# 允许最大连接人数
max_user_connections=1000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用"mysql_native_password"插件认证解决客户端无法连接的问题
default_authentication_plugin=mysql_native_password
2. 启动和停止
1. 查看运行状态
shell
root@jinshengyuan:~# systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-03 09:22:43 CST; 6min ago
Main PID: 1876 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4423)
Memory: 361.3M
CGroup: /system.slice/mysql.service
└─1876 /usr/sbin/mysqld
Nov 03 09:22:43 iZ2vcaf13jzrdbxaik2h6nZ systemd[1]: Starting MySQL Community Server...
Nov 03 09:22:43 iZ2vcaf13jzrdbxaik2h6nZ systemd[1]: Started MySQL Community Server.
root@jinshengyuan:~#
2. 开机自启
MySQL服务器在安装后自动启动。 您可以使用 以下命令:
shell
root@jinshengyuan:~# systemctl status mysql
3. 查看默认服务器配置命令
shell
mysqld --verbose --help
3. 登录
shell
sudo mysql
sudo mysql -u root