1.下载mysql8.0
wget https://cdn.mysql.com//archives/mysql-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
2.解压
tar xf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
mv mysql-8.0.11-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local/
3.创建mysql用户组和用户
sudo groupadd mysql
sudo useradd -g mysql mysql
4.改变mysql目录权限
sudo chown -R mysql.mysql /usr/local/mysql/
5.初始化mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
可能出现以下错误:
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
可以安装:
sudo apt install libaio*
输出如下:
2023-02-23T02:16:00.851836Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 21653
2023-02-23T02:16:11.853475Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 9JZWudsxYc#s
2023-02-23T02:16:21.064487Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server has completed
注意:这里要记住mysql的密码,后面修改密码需要
6.修改配置文件
a.首先通过命令查看mysql读物配置文件顺序:
bin/mysql --help | grep my.cnf
显示如下:
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cn
b.修改配置文件
sudo vim /etc/mysql/my.cnf
c.改成如下
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
character-set-server=utf8
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-name-resolve
[client]
socket = /usr/local/mysql/mysql.sock
default-character-set=utf8
7.建立mysql服务
cp /user/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
8.修改环境变量
a.在/etc/profile追加
export PATH=$PATH:/usr/local/mysql/bin
b.生效
source /etc/profile
9.启动mysql
mysqld
10.修改mysql密码
a.安装客户端
sudo apt install mysql-client
b.使用上面的密码登录
mysql -uroot -p9JZWudsxYc#s
c.修改密码
alter user 'root'@'localhost' identified by '123456';
11.再使用新设置的密码登录即可