上期我们分享了CRMEB多商户系统(Java)升级MySQL 8的完整攻略,其中提到一个常见问题——如果你的服务器内存只有4G,或安装了宝塔这类面板,可能直接安装MySQL 8会失败。
当时我们建议:可以通过命令行手动编译安装,再外链到面板进行管理。
是不是听起来有点麻烦?别急,今天就是来助攻的!
这篇文章手把手带你在CentOS服务器上编译安装MySQL 8,专治各种环境不符、内存不足、安装失败。如果你也卡在这个环节,赶紧往下看吧!
1、下载MySQL 8安装包
官方地址:https://dev.mysql.com/downloads/mysql/
2、上传、解压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
3、创建用户组以及密码
groupadd mysql
useradd -g mysql mysql
#授权用户
chown -R mysql.mysql /usr/local/mysql-8.0.34
4、切换到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
Tips:☝️得到临时密码,这里的密码记得保存‼️
5、编辑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
6、添加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
7、启动MySQL
systemctl start mysql
#查看启动状态
systemctl status mysql
#将mysql命令添加到服务
ln -s /usr/local/mysql-8.0/bin/mysql /usr/bin
8、登录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>
9、修改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位。
10、修改远程链接并生效
#进入到mysql
use mysql;
update user set host='%' where user='root';
flush privileges;