Linux Mysql5.7安装教程(亲测)

Mysql5.7安装

一、安装前准备

前往MySQL官网下载对应的安装包,下载页面地址:
https://downloads.mysql.com/archives/community/
上面可以选择相应操作系统和版本


除此之外,也可以直接通过 https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz地址直接在 Linux 服务器上下载:

content_copy
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

个人建议复制下载链接到迅雷上下载比较快

二、安装

创建mysql用户,会在/home下生成mysql用户目录

content_copy
useradd mysql
content_copy
cd /home/mysql

解压

content_copy
tar -xvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

更改加压后的文件名

content_copy
 mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql57

进入mysql57目录

content_copy
cd /home/mysql/mysql57

创建目录,用于后面数据存放

content_copy
mkdir data log conf

进入conf创建my.cnf,配置后以后就不使用/etc/my.cnf配置

content_copy
cd /home/mysql/mysql57/conf
vim my.cnf
content_copy
[client]
socket=/home/mysql/mysql57/mysql.sock
[mysqld]
port=3106 #端口自己定义
basedir=/home/mysql/mysql57
datadir=/home/mysql/mysql57/data
pid-file=/home/mysql/mysql57/mysql.pid
socket=/home/mysql/mysql57/mysql.sock
log_error=/home/mysql/mysql57/log/error.log
server-id=100
log_bin=/home/mysql/mysql57/binlog/mysql-bin
symbolic-links=0
init-connect='SET NAMES utf8mb4' 
character-set-server=utf8mb4
lower_case_table_names=1
[mysqld_safe]
log-error=/home/mysql/mysql57/log/error.log
pid-file=/home/mysql/mysql57/mysql.pid

修改mysql57权限为mysql

content_copy
chown -R mysql:mysql /home/mysql/mysql57

初始化Mysql

content_copy
cd /home/mysql/mysql57
 ./bin/mysqld --defaults-file=/home/mysql/mysql57/conf/my.cnf --initialize --user=mysql --basedir=/home/mysql/mysql57 --datadir=/home/mysql/mysql57/data

执行时出现

content_copy
./bin/mysqld: error while loading shared libraries: libaio.so.1:

直接执行以下命令完后再重新初始化

content_copy
yum install -y libaio

通过以下命令查看安装日志

content_copy
tail -f /home/mysql/mysql57/log/error.log 

日志出现

content_copy
2022-01-12T16:05:02.637084Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server op
tion (see documentation for more details).
mysqld: File '/home/mysql/mysql57/binlog/mysql-bin.index' not found (Errcode: 2 - No such file or directory)
2022-01-12T16:05:02.639335Z 0 [ERROR] Aborting

执行以下命令解决

content_copy
 mkdir -p /home/mysql/mysql57/binlog/
 touch /home/mysql/mysql57/binlog/mysql-bin.index
 chown -R mysql:mysql /home/mysql/mysql57

再次查看error.log日子,出现以下日志则初始化成功

content_copy
tail -f /home/mysql/mysql57/log/error.log 

2022-01-12T16:09:23.590301Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-01-12T16:09:24.467539Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-01-12T16:09:24.579078Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-01-12T16:09:24.646209Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 029eec3a-73c2-11ec-a7dd-00163e149163.
2022-01-12T16:09:24.647937Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-01-12T16:09:25.358771Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-01-12T16:09:25.358784Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-01-12T16:09:25.359461Z 0 [Warning] CA certificate ca.pem is self signed.
2022-01-12T16:09:25.895400Z 1 [Note] A temporary password is generated for root@localhost: Yk4F=m&ZGDk+

三、启动Mysql Server

content_copy
 ./bin/mysqld_safe --defaults-file=/home/mysql/mysql57/conf/my.cnf --user=mysql &

期间提示(废弃)

content_copy
2022-01-13T13:52:27.555079Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.

解决方法(废弃)

content_copy
mkdir -p /var/log/mariadb/
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/

期间提示(废弃)

content_copy
2022-01-13T13:53:46.919209Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.

解决方法(废弃)

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

期间提示(废弃)

content_copy
2022-01-13T14:00:42.003092Z mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended

解决方法(废弃)

content_copy
mkdir /var/log/mariadbmkdir /var/run/mariadbchown
chown -R mysql:mysql /var/log/mariadbmkdir
chown -R mysql:mysql /var/run/mariadbchown

四、登录与授权

登录

content_copy
./bin/mysql --defaults-file=/home/mysql/mysql57/conf/my.cnf -uroot -p

这里密码,就是安装时初始化打印在日志上Yk4F=m&ZGDk+
设置新密码

content_copy
alter user user() identified by "密码";

配置远程访问

content_copy
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'new_pass_word' WITH GRANT OPTION;

这里的%可以配置成你允许访问的IP,这里配置%为所有主机都可以访问
刷新配置生效

content_copy
FLUSH PRIVILEGES;

这里全部操作完成

posted @ 2022-01-13 22:49  灰辉-灰  阅读(1930)  评论(0编辑  收藏  举报