Centos 安装MySQL 5.7.38

1、下载MySQL安装包

下载所需的rpm包https://dev.mysql.com/downloads/mysql/
Centos 安装MySQL 5.7.38_第1张图片

2、检查是否已经安装mysql或者mariadb

[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -qa | grep mariadb

如果安装过,上面命令会检查出相关安装信息,将其卸载

#卸载MySQL
rpm -qa | grep mysql | xargs  rpm -e --nodeps
#卸载mariadb
rpm -qa | grep mariadb| xargs  rpm -e --nodeps

3、依次安装如下4个文件即可

[root@localhost mysql]# rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm

在安装server文件时,可能还会提示缺失其他依赖包,比如:net-tools,根据提示逐一下载相关依赖包并安装,全部依赖解决完后,再安装mysql-community-server这个rpm文件。缺失的依赖包推荐到这里下载:https://centos.pkgs.org/7/centos-x86_64/

至此,mysql5.7所有文件安装完毕,接下来就是开启服务了。

4、修改配置文件

修改配置文件my.cnf,设置免密登陆

[root@localhost ~]# vim /etc/my.cnf
### 可以参考如下内容
[mysql]
default-character-set=utf8

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
slow_query_log = ON
slow_query_log_file = /var/log/mysql_slow_query.log
long_query_time = 3
symbolic-links=0
log-error=/data/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin
thread_cache_size=100
server-id=33
back_log=500
lower_case_table_names=1
#thread_concurrency=80
#thread_concurrency=32
query_cache_limit=12M
query_cache_size=512M
query_cache_type=1
character-set-server=utf8mb4
max_connections=3000
innodb_buffer_pool_size=12G
innodb_file_per_table=ON
innodb_log_file_size=3G
expire_logs_days=5
read_only=1
lower_case_table_names=1
skip-name-resolve
[client]
socket=/data/mysql/mysql.sock

5、启动MySQL

systemctl restart mysqld

启动时间较长,需要耐心等待,预计再10分钟左右,因为需要数据库初始化。

6、查看MySQL初始密码

通过my.cnf文件找到/data/mysql/mysqld.log文件,在改文件中根据password关键字查询初始密码

#查看log-error指定的数据目录
[root@admin02 ~]# cat /etc/my.cnf | grep log-error
log-error=/data/mysql/mysqld.log

#查看初始化密码
[root@admin02 ~]# cat /data/mysql/mysqld.log | grep localhost
2022-07-05T16:48:26.206485Z 1 [Note] A temporary password is generated for root@localhost: B9eldAjOiu=(

查看密码后,就可以使用该密码登录到MySQL,并进行后续的操作

7、登录MySQL

[root@admin02 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38-log MySQL Community Server (GPL)

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> set global validate_password_policy = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

安装完成!

你可能感兴趣的:(Linux,MySQL,mysql,centos,linux)