Centos安装Mysql

本文通过下载安装包方式进行安装,Centos版本7.9,Mysql版本8.0

下载安装包

1、进入网址 https://dev.mysql.com/downloads/mysql/
image

2、选择对应版本进行下载,Centos系统选择Red Hat选项
image

3、提示要登陆,点击No Thanks,just start my download进行下载
image

卸载Centos自带的Mariadb

1、通过 rpm -qa | grep mariadb 命令查看 mariadb 的安装包
image

2、通过 rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps 命令装卸 mariadb
image
看到控制台没有任何输出是正常的

3、通过 rpm -qa | grep mariadb 命令再次查看 mariadb 的安装包,没有看到即可
image

检查是否有libaio库

MySQL 依赖于该libaio 库。如果未在本地安装此库,则数据目录初始化和后续服务器启动步骤将失败。
yum search libaio
yum install libaio # install library

安装Mysql安装包

1、通过 cd /usr/local/ 命令进入根目录下的usr目录下的local目录,这个目录是放一些本地的共享资源的。并且通过mkdir mysql命令在当前目录下创建一个名为mysql的目录
image

2、将下载好的安装装包传输到该目录中
image

3、通过tar -xvf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar解压安装包
image

4、通过rpm -ivh mysql-community-{模块名}-8.0.30-1.el7.x86_64.rpm --nodeps --force 按顺序安装common、libs、client、server,最后通过rpm -qa | grep mysql 命令查看 mysql 的安装包,结果如下
image

5、通过以下命令,完成对 mysql 数据库的初始化和相关配置
mysqld --initialize 注意如果需要数据库支持忽略大小写则写成 mysqld --initialize --lower-case-table-names=1 因为Mysql8.0只支持在初始化时做修改,否则后期只能重新初始化!
chown mysql:mysql /var/lib/mysql -R
systemctl start mysqld.service
systemctl enable mysqld
image

设置mysql用户和远程连接

1、通过 cat /var/log/mysqld.log | grep password 命令查看数据库的密码
image

2、通过 mysql -uroot -p 敲回车键进入数据库登陆界面,并输入刚才查看到的数据库密码
image
看到这个界面就成功登录到mysql了

3、通过 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456' 命令来修改密码,我这里以修改密码成123456为例子。
image

4、通过 exit; 命令退出 MySQL,然后通过新密码再次登陆 mysql -uroot -p123456
再通过以下命令授权远程登陆
create user 'root'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
image

5、通过 exit; 命令退出 MySQL,通过以下命令,关闭 firewall
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl mask firewalld.service
image

6、通过连接工具测试连接,连的上就说明设置成功啦!
image

修改Mysql配置

通过以上安装方式,mysql配置文件路径为/etc/my.cnf
如果需要修改配置文件,输入命令systemctl restart mysqld重启即可生效

posted @ 2022-09-13 21:06  jasonX1an  阅读(11144)  评论(1编辑  收藏  举报