linux mysql 5.7 离线安装(tar.gz)
1. 官网下载压缩包
mysql 下载地址
https://downloads.mysql.com/archives/community/
5.7最新安装包下载
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
2. 查询并卸载系统自带的Mariadb
rpm -qa | grep mariadb
rpm -e --nodeps 文件名
3. 将压缩包放入服务器安装目录下(/usr/local),解压,重命名为mysql
#解压命令
tar -xzvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
#重命名
mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql
4. 检测mysql用户和mysql用户组是否存在,不存在则创建
#检测是否存在,如无则创建
cat /etc/group | grep mysql
cat /etc/passwd | grep mysql
*若存在请忽略创建
#创建mysql用户组
groupadd mysql
#创建一个用户名为mysql的用户,并加入mysql用户组
useradd -g mysql mysql
#修改mysql用户的登陆密码(不少于8位)
passwd mysql
5. 修改mysql文件夹下所有文件的用户和用户组
chown -R mysql:mysql mysql/
6. 创建mysql配置文件 my.cnf(位置/etc下面)
cd /etc
vi my.cnf
#将下面的my.cnf的内容复制进来,并保存
my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 设置mysql客户端默认字符集
socket=/var/lib/mysql/mysql.sock
[mysqld]
#skip-name-resolve
#设置3306端口
port = 3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp/mysql
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#lower_case_table_name=1
max_allowed_packet=16M
user=mysql
7. 创建my.cnf中配置的相关目录
#创建目录
mkdir /var/lib/mysql
#赋权限
chmod 777 /var/lib/mysql
8. 初始化mysql数据库
/usr/local/mysql/bin/mysqld --initialize --user=mysql
报错如下:
解决方式:yum -y install numactl
9. 配置mysql服务开机自启动
#复制启动脚本到资源目录
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
#增加mysqld服务控制脚本执行权限
chmod +x /etc/rc.d/init.d/mysqld
# 将mysqld服务加入到系统服务
chkconfig --add mysqld
# 检查mysqld服务是否已经生效
chkconfig --list mysqld
# 切换至mysql用户,启动mysql
service mysqld start
#启动成功如下图:
10. 配置环境变量
vim /etc/profile
#最下面增加如下
export PATH=$PATH:/usr/local/mysql/bin
#刷新环境变量使之生效
source /etc/profile
11. 修改mysql密码(废了老劲了)
#获取root用户密码,若没有该文件,往下看有解决方式
cat /root/.mysql_secret
#登陆mysql
mysql -uroot -p
#输入刚才的密码,回车报错如下:
解决方式: ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
- 重启mysql服务,再次输入密码,又报错了。
默认的密码不好使或者没有默认密码,如下解决:
#停止mysql服务
service mysqld stop
#以不启用grant-tables模式启动mysql
mysqld_safe --skip-grant-tables &
mysql -uroot -p 直接回车进去
use mysql;
#更新密码如下,5.7版本下的mysql数据库下已经没有password这个字段了,password字段改成了authentication_string
update user set authentication_string=password('111111') where user='root';
#刷新权限
flush privileges;
12. 给mysql赋远程连接权限
#登陆mysql
mysql -uroot -p
#选择mysql
use mysql
#若报错如下,需要重置密码。
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#重置密码
SET PASSWORD = PASSWORD('111111');
#设置密码永不过期
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
#赋远程访问权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '111111';
#刷新
FLUSH PRIVILEGES;