Centos7 安装Mysql8.0
Posted BasicLab基础架构实验室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 安装Mysql8.0相关的知识,希望对你有一定的参考价值。
1、到指定目录下下载安装包
[root@VM-0-14-centos ~]# cd /usr/local/src
![](https://image.cha138.com/20230312/cfad87c5c98246e6a9214e8391e8af5f.jpg)
2、下载mysql8
[root@VM-0-14-centos src]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
![](https://image.cha138.com/20230312/eb024b2a3306416ab9cbe9a44e6e7c02.jpg)
3、解压mysql8, 通过xz命令解压出tar包, 然后通过tar命令解压出文件夹
[root@VM-0-14-centos src]# xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz[root@VM-0-14-centos src]# tar xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar
![](https://image.cha138.com/20230312/56aba8cbb7ed40638d231c77aec7b6dd.jpg)
![](https://image.cha138.com/20230312/1f15fc642f8b44b583143e01f3171f61.jpg)
4、将/usr/local/src下的mysql-8.0.20-linux-glibc2.12-x86_64文件夹内容移动到/usr/local/mysql下
[root@VM-0-14-centos src]# mkdir /usr/local/mysql[root@VM-0-14-centos src]# mv /usr/local/src/mysql-8.0.20-linux-glibc2.12-x86_64/*/usr/local/mysql[root@VM-0-14-centos src]# cd /usr/local/mysql
![](https://image.cha138.com/20230312/ca45a18c84974cd095e579692dfab9aa.jpg)
5、创建用户组及用户和密码
[root@VM-0-14-centos mysql]# groupadd mysql[root@VM-0-14-centos mysql]# useradd -g mysql mysql
![](https://image.cha138.com/20230312/e2709031e9c942a19e7683639e506cbf.jpg)
6、授权用户
[root@VM-0-14-centos mysql]# chown -R mysql.mysql /usr/local/mysql
![](https://image.cha138.com/20230312/6b551e62079f40008b61b7b3ff99b7f8.jpg)
7、编辑my.cnf文件
[root@VM-0-14-centos mysql]# vim /etc/my.cnf
按大写 i 进入修改模式 然后将下面的文本复制进去
[mysqld]user=rootdatadir=/usr/local/mysql/databasedir=/usr/local/mysqlport=3306max_connections=200max_connect_errors=10character-set-server=utf8default-storage-engine=INNODBdefault_authentication_plugin=mysql_native_passwordlower_case_table_names=1group_concat_max_len=102400[mysql]default-character-set=utf8[client]port=3306default-character-set=utf8
![](https://image.cha138.com/20230312/2c990417c0264711957e648575880409.jpg)
复制进去后按esc退出修改模式,然后按 shift+: 然后输入wq保存文件
8、进入到bin目录下
[root@VM-0-14-centos mysql]# cd bin
![](https://image.cha138.com/20230312/08ed2c793e09483ba1462fde3f514359.jpg)
9、初始化基础信息,最后一行后面会有个随机的初始密码保存下来一会登录要用(如果忘记了就删掉data重新初始化)
[root@VM-0-14-centos bin]# ./mysqld --initialize
如果提示 :./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
![](https://image.cha138.com/20230312/e669e6b714d246258a3e9bbc9d607748.jpg)
就执行下下面这个再执行初始化
[root@VM-0-14-centos bin]# yum install -y libaio[root@VM-0-14-centos bin]# yum -y install numactl
![](https://image.cha138.com/20230312/395032a37c4d4f019c7297ed6855dada.jpg)
![](https://image.cha138.com/20230312/a1339ee44fa942bb8f8208a40eeb76a8.jpg)
[root@VM-0-14-centos bin]# ./mysqld --initialize
![](https://image.cha138.com/20230312/768f62e5fd09466eb1c4ac8d4bc762e6.jpg)
记录日志最末尾位置root@localhost:后的字符串,此字符串为mysql管理员临时登录密码
10、添加mysqld服务到系统
先返回到mysql目录
[root@VM-0-14-centos bin]# cd ..
![](https://image.cha138.com/20230312/64bed2d1fbff4b059e479de1cdf4b3a2.jpg)
[root@VM-0-14-centos mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysql
![](https://image.cha138.com/20230312/4c86c283ba774b279ccd17227e3d709a.jpg)
11、授权以及添加服务
[root@VM-0-14-centos mysql]# chmod +x /etc/init.d/mysql[root@VM-0-14-centos mysql]# chkconfig --add mysql
![](https://image.cha138.com/20230312/3fa4fff90cd0489d9cd1ae70f24e2c14.jpg)
12、启动mysql
[root@VM-0-14-centos mysql]# service mysql start
![](https://image.cha138.com/20230312/18846cc2bbde46c9bc98ff174acab6b6.jpg)
13、将mysql添加到命令服务
[root@VM-0-14-centos mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin
![](https://image.cha138.com/20230312/dc5b754749b641e6b7e13e69e2d78835.jpg)
14、登录mysql
[root@VM-0-14-centos mysql]# mysql -uroot -p
如果出现 mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
cd /usr/local/mysql/binldd mysql
![](https://image.cha138.com/20230312/e2c7af653444473b92362bdddeaf8841.jpg)
sudo ln -s /usr/lib64/libtinfo.so.6.1/usr/lib64/libtinfo.so.5
15、输入刚刚初始化时生成的密码
![](https://image.cha138.com/20230312/35fb8502cd6b452b9e7ccf1535d0c1f9.jpg)
16、更改root用户密码, 注意语句后的; 执行语句忘记写了 可以补个空的;回车也可以将语句执行
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY'你的密码';
![](https://image.cha138.com/20230312/ebc106ce98a840029e1a051a6f9fbf3f.jpg)
mysql> flush privileges;
![](https://image.cha138.com/20230312/c5f9f7b107354c52b90b19d945d11eae.jpg)
17、更改root连接权限
mysql>use mysql;mysql> update user set host='%' where user ='root';mysql> flush privileges;
![](https://image.cha138.com/20230312/9a9db2a1faec465283af5611f35385ca.jpg)
18、exit; 退出mysql,现在就可以通过连接工具登录root账户进行远程连接了
![](https://image.cha138.com/20230312/940d0eb357374d99b6755328cddc10be.jpg)
至此,mysql8.0.20版本的数据库安装,已经完成。系统为CentOS Linux release 7.8.2003 (Core)
以上是关于Centos7 安装Mysql8.0的主要内容,如果未能解决你的问题,请参考以下文章