AnolisOS 8 MySQL8 dnf安装与配置

一、MySQL安装

1.1、查看MySQL安装包信息
dnf list *mysql*|grep mysql
1.2、MySQL安装命令
dnf install mysql-server.x86_64
1.3、启动和设置开机启动MySQL服务
[root@anolis8 ~]# systemctl start mysqld   #启动服务
[root@anolis8 ~]# systemctl is-enabled mysqld    #查看是否设置开机启动
disabled
[root@anolis8 ~]# systemctl enable mysqld     #设置mysqld随开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
1.4、查看MySQL服务运行状态
[root@anolis8 ~]# systemctl status mysqld -l
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-10-06 11:39:21 CST; 5min ago
 Main PID: 3086 (mysqld)
   Status: "Server is operational"
    Tasks: 37 (limit: 23664)
   Memory: 453.5M
   CGroup: /system.slice/mysqld.service
           └─3086 /usr/libexec/mysqld --basedir=/usr

10月 06 11:39:11 anolis8 systemd[1]: Starting MySQL 8.0 database server...
10月 06 11:39:11 anolis8 mysql-prepare-db-dir[3001]: Initializing MySQL database
10月 06 11:39:21 anolis8 systemd[1]: Started MySQL 8.0 database server.

二、MySQL配置

2.1、MySQL登录

本CentOS8教程使用默认管理账号root登录MySQL,进行MySQL配置,输入如下命令后根据提示输入密码即可。

#先查看mysql日志
[root@anolis8 ~]# cat /var/log/mysql/mysqld.log
2022-10-06T03:39:12.013073Z 0 [System] [MY-013169] [Server] /usr/libexec/mysqld (mysqld 8.0.26) initializing of server in progress as process 3037
2022-10-06T03:39:12.042817Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-06T03:39:13.558719Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-06T03:39:15.363847Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-10-06T03:39:15.364519Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-10-06T03:39:15.500261Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-10-06T03:39:20.520386Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld 8.0.26) starting as process 3086
2022-10-06T03:39:20.573373Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-06T03:39:21.162109Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-06T03:39:21.568841Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-10-06T03:39:21.569064Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-10-06T03:39:21.570891Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-10-06T03:39:21.571166Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-10-06T03:39:21.603224Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/lib/mysql/mysqlx.sock
2022-10-06T03:39:21.603312Z 0 [System] [MY-010931] [Server] /usr/libexec/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution.

#初始密码为空
root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. 

[root@anolis8 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, 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> 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

2.2、切换当前库为mysql
use mysql;
2.3、刷新权限,无需重启即可生效
flush privileges;
2.4、修改原始密码
第一步,此处采用 mysql_native_password 方式,数据库默认为 localhost 本地登录,如需远程登录请替换为指定IP或路径。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass123';
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select host,user from user where user='root';
+-----------+------+
| host      | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
第二步,打开MySQL配置文件,修改MySQL配置参数,然后刷新权限或重启MySQL服务生效。

打开配置文件

vi /etc/my.cnf.d/mysql-server.cnf
#修改参数
[mysqld]
找到:default_authentication_plugin=caching_sha2_password
改为:default_authentication_plugin=mysql_native_password
第三步,刷新或者重启生效。
systemctl restart mysqld;
2.5、修改MySQL配置的默认编码
vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
character-set-server=utf8mb4
2.6、修改MySQL配置的默认端口
vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
port=53306
2.7、MySQL配置的日志
vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
log-error=/tmp

三、MySQL常用命令

3.1、使用命令操作数据库前,先简单的了解下数据库的一些概念

在MySQL中将权限管理分为三类:

数据权限:增删查改(select\update\delete\insert)
结构权限:结构操作(create\drop)
管理权限:权限管理(create user\grant\revoke)
我们常用的增删改查以及删库等操作,还有一些权限管理。例如授予权限:grant,取消权限:revoke,刷新权限:flush等,会在下面举例说明。

3.2、创建账号,并设置全部权限

用户名自己设置,主机名一般是内外网IP或者本机localhost,密码尽量复杂一些,数据库名为自己创建的数据库。

  • 创建一个新用户
    CREATE USER 用户名@'主机名' IDENTIFIED BY '密码';
  • 为用户设置一个数据库的权限
  • GRANT ALL PRIVILEGES on 数据库名.* to 用户名@'主机名';
[root@anolis8 ~]# mysql -hlocalhost -P3309 -uroot -ppass123
mysql> create user 'mha'@'%' identified with mysql_native_password by 'pass123456';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all privileges on *.* to 'mha'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
3.3、使用账号登录指定IP

如果仅限内网登录,无需配置防火墙,如果需要远程登录,需要开放防火墙的3306端口,并设置开放第三方的安全组等。

  • 远程登录需要开放端口,默认为3306
firewall-cmd --zone=public --add-port=3306/tcp --permanent
  • mysql -h主机名 -P端口 -u用户名 -p密码
  • 例如,登录密码为pass123456
[root@anolis8 ~]# mysql -hlocalhost -P3309 -uroot -ppass123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution

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

3.4、彻底卸载MySQL
  • 第一步,首先停止服务
systemctl stop mysqld
  • 第二步,查看已安装的 MySQL
[root@anolis8 ~]# dnf list installed mysql*
Repository epel is listed more than once in the configuration
已安装的软件包
mysql.x86_64                                                         8.0.26-1.0.1.module+an8.4.0+10570+ea0623f5                                                   @AppStream
mysql-common.x86_64                                                  8.0.26-1.0.1.module+an8.4.0+10570+ea0623f5                                                   @AppStream
mysql-errmsg.x86_64                                                  8.0.26-1.0.1.module+an8.4.0+10570+ea0623f5                                                   @AppStream
mysql-server.x86_64                                                  8.0.26-1.0.1.module+an8.4.0+10570+ea0623f5                                                   @AppStream
  • 第三步,卸载服务
dnf remove mysql.x86_64 mysql-common.x86_64 mysql-errmsg.x86_64 mysql-server.x86_64
  • 第四步,彻底删除遗留文件,如果修改过配置路径,请按照实际路径替换,否则按照默认路径修改即可
#安装目录
rm -rf /var/lib/mysql
#sock文件
rm -rf /var/lib/mysql/mysql.sock
#日志文件
rm -rf /var/log/mysql/mysqld.log
#进程号文件
rm -rf /run/mysqld/mysqld.pid
#默认配置文件
rm -rf /etc/my.cnf
#系统创建的配置文件的存放路径
rm -rf /etc/my.cnf.d/

你可能感兴趣的:(AnolisOS 8 MySQL8 dnf安装与配置)