Centos服务器编译安装Mysql8数据库

86 阅读2分钟

Centos服务器安装MYSQL8数据库

一、下载mysql8.0包

官方地址:dev.mysql.com/downloads/m…

image.png

二、上传、解压mysql

将下载好的mysql包上传到/usr/local/目录下

根据包的后缀选择解压语句

tar -zxvf mysql-8.0.43-linux-glibc2.28-x86\_64.tar.gz
或者
tar -xvf mysql-8.0.43-linux-glibc2.28-x86\_64.tar.xz

目录改名

mv  mysql-8.0.43-linux-glibc2.28-x86\_64 mysql-8.0.43
#创建data文件夹 存储文件

cd mysql-8.0.34
mkdir data

三、创建用户组以及密码

groupadd mysql
useradd -g mysql mysql
#授权用户

chown -R mysql.mysql /usr/local/mysql-8.0.34

四、切换到bin目录下,安装mysql

cd bin

./mysqld --user=mysql --lower\_case\_table\_names=1 --basedir=/usr/local/mysql-8.0.34 --datadir=/usr/local/mysql-8.0.34/data/ --initialize

image.png

得到临时密码,这里的密码记得保存

五、编辑my.cnf文件

vi /etc/my.cnf

添加以下内容:

[mysqld]

basedir=/usr/local/mysql-8.0/

datadir=/usr/local/mysql-8.0/data/

socket=/tmp/mysql.sock

character-set-server=UTF8MB4

symbolic-links=0

lower\_case\_table\_names=1

六、添加mysqld服务到系统

cd /usr/local/mysql-8.0.34

cp -a ./support-files/mysql.server /etc/init.d/mysql
#授权以及添加服务

 

chmod +x /etc/init.d/mysql

chkconfig --add mysql

七、启动mysql

systemctl start mysql

#查看启动状态

systemctl status mysql

#将mysql命令添加到服务
ln -s /usr/local/mysql-8.0/bin/mysql /usr/bin

八、登录mysql

mysql -uroot -p 密码使用之前随机生成的密码

mysql -uroot -p

操作记录

mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.34

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>

九、修改root密码

两种方式:

第一种:

[root\@mysql-server \~]# mysql -uroot -p'woHtkMgau9,w'   #登录

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 2

Server version: 5.7.27

....

mysql> alter user 'root'@'localhost' identified by 'Yang\@123';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;     //刷新权限表

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

\[root\@mysql-server \~]# mysql -uroot -p'Yang\@123'

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 3

Server version: 5.7.27 MySQL Community Server (GPL)

...

mysql> exit

Bye

第二种:

mysqladmin -u root -p'旧密码' password '新密码'

 注:修改密码必须大小写字母、数字和特殊符号都有,长度不能小于8位

十、修改远程链接并生效

#进入到mysql


use mysql;

update user set host='%' where user='root';

flush privileges;

十一、常见问题

1.RROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 2ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords