CentOS7.6安装MYSQL8.0的步骤详解

1、一般CentOS默认安装了mariadb,所以先查看是否安装mariadb,如果安装就需要先卸载mariadb

rpm -qa|grep mariadb
rpm -e mariadb-libs --nodeps

执行后效果:

[root@xuhangCentos7 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64 [root@xuhangCentos7 ~]# rpm -e
mariadb-libs --nodeps [root@xuhangCentos7 ~]# rpm -qa | grep mariadb
[root@xuhangCentos7 ~]#

2、在/usr/local/目录下创建mysql文件夹

cd /usr/local/
mkdir mysql

3、进入mysql文件夹内,下载mysql

cd mysql
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

运行效果:

[root@xuhangCentos7 mysql]# clear [root@xuhangCentos7 mysql]# cd
/usr/local/mysql/ [root@xuhangCentos7 mysql]# ls
mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar [root@xuhangCentos7 mysql

]#

4、解压mysql到当前目录

tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

运行效果:

[root@xuhangCentos7 mysql]# tar -xvf
mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
mysql-community-libs-8.0.16-2.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.16-2.el7.x86_64.rpm
mysql-community-devel-8.0.16-2.el7.x86_64.rpm
mysql-community-server-8.0.16-2.el7.x86_64.rpm
mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
mysql-community-client-8.0.16-2.el7.x86_64.rpm
mysql-community-common-8.0.16-2.el7.x86_64.rpm
mysql-community-test-8.0.16-2.el7.x86_64.rpm [root@xuhangCentos7
mysql]#

5、安装mysql,依次执行下列命令

安装 mysql-community-common-8.0.16-2.el7.x86_64.rpm

rpm -ivh mysql-community-common-8.0.16-2.el7.x86_64.rpm --nodeps --force

安装 mysql-community-libs-8.0.16-2.el7.x86_64.rpm

rpm -ivh mysql-community-libs-8.0.16-2.el7.x86_64.rpm --nodeps --force

安装 mysql-community-client-8.0.16-2.el7.x86_64.rpm

rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm --nodeps --force 

安装 mysql-community-server-8.0.16-2.el7.x86_64.rpm

rpm -ivh mysql-community-server-8.0.16-2.el7.x86_64.rpm --nodeps --force

安装完成执行 rpm -qa | grep mysql 查看,会有你安装的四个

[root@xuhangCentos7 mysql]# rpm -qa | grep mysql
mysql-community-common-8.0.16-2.el7.x86_64
mysql-community-libs-8.0.16-2.el7.x86_64
mysql-community-server-8.0.16-2.el7.x86_64
mysql-community-client-8.0.16-2.el7.x86_64 [root@xuhangCentos7 mysql]#

6、初始化MySQL

执行 mysqld --initialize

mysqld --initialize

[root@xuhangCentos7 mysql]# mysqld --initialize

这个命令执行后会在/var/lib/下生成一个mysql文件夹,这个文件夹就是mysql的data,同时也会生成一个用户名为 root 和一个初始化密码,密码在 /var/log/mysqld.log文件中,可以使用 cat /var/log/mysqld.log | grep password 命令进行查找

密码正常是12位,一定要记住这个密码,我本次密码如下 kgua=ew(*2(X

cat /var/log/mysqld.log | grep password 

[root@xuhangCentos7 mysql]# cat /var/log/mysqld.log | grep password
2021-02-04T03:13:57.116717Z 5 [Note] [MY-010454] [Server] A temporary
password is generated for root@localhost: kgua=ew(*2(X
[root@xuhangCentos7 mysql]#

注意:到这里有的人可能会出现一个问题,就是生成初始化密码失败,这是因为你初始化之前没有删除/var/lib/mysql这个文件夹
初始化mysql后执行 chown mysql:mysql /var/lib/mysql -R;给data分配权限

chown mysql:mysql /var/lib/mysql -R

[root@xuhangCentos7 mysql]# chown mysql:mysql /var/lib/mysql -R
[root@xuhangCentos7 mysql]#

7、启动mysql服务

这里需要注意,如果你的linux是linux 6点几,就使用service mysqld start

service mysqld start

如果你的linux是linux 7点几可以使用systemctl start mysqld.service;

systemctl start mysqld.service

运行效果:

[root@xuhangCentos7 mysql]# systemctl start mysqld.service
[root@xuhangCentos7 mysql]#

8、登录MySQL

mysql -u root -p

输入你刚刚记下的密码

如果你登不上,而且打印信息有mysql.sock 那你就配置/etc/my.cnf文件,如果你发现没有my.cnf,使用touch my.cnf创建文件

touch /etc/my.cnf

然后使用:

vim /etc/my.cnf

文件中写入:

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
port = 3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
no-auto-rehash
default-character-set=utf8

添加完重新启动mysql服务

service mysqld restart

10、修改密码: ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’ 这里修改密码为root

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

11、退出mysql,然后通过新密码再次登陆验证密码是否修改成功

exit; 
mysql -uroot -p

运行效果:

mysql> exit; Bye [root@xuhangCentos7 mysql]# mysql -uroot -p Enter
password: Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9 Server version: 8.0.16 MySQL Community
Server - GPL

Copyright © 2000, 2019, Oracle and/or its affiliates. All rights
reserved.

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>

12、配置远程访问

把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户,并且密码是root

create user 'root'@'%' identified with mysql_native_password by 'root';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;

13、 添加账户

为了安全起见,强烈建议在远程连接的时候使用非root账户。创建test用户,密码为“MyNewPass1”,为其分配所有权限。

14、添加完重新启动mysql服务

service mysqld restart

15. 设置开机启动

systemctl enable mysqld

16. 开放端口

sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports

17、解决乱码问题

配置/etc/my.cnf
使用命令:vim /etc/my.cnf

vim /etc/my.cnf

在文件开始处添加

[clent]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8

在[mysqld]处添加

port = 3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8

在文件结尾处添加:

[mysql]
no-auto-rehash
default-character-set=utf8

18、测试连接数据库

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值