# Linux mysql安装 5.7解压版

# 目录位置约定

安装文件下载目录:/home/data/software

Mysql目录安装位置:/usr/local/mysql

数据库保存位置:/home/data/mysql

# 安装文件上传

mkdir -p /home/data/software

  1. 新建安装文件下载目录

    mkdir -p /home/data/software

  2. 新建数据库保存位置

    mkdir –p /home/data/mysql

# 解压安装包文件

  1. 进入目录

    cd /home/data/software

  2. 解压

    tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

  3. 移动目录并改名

    mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

# 新建mysql用户

  1. 新建用户组

    /usr/sbin/groupadd mysql

  2. 新建用户 禁止登录ssh

    useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql

# 目录赋权

cd /usr/local/mysql
chown -R mysql /usr/local/mysql/
chgrp -R mysql /usr/local/mysql/
chmod -R 755 /home/data/mysql #新增 看FAQ1
1
2
3
4

# 参数配置

此处会生成临时密码,在结尾处,需记录

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/home/data/mysql/

bin/mysql_ssl_rsa_setup --datadir=/home/data/mysql/

# 修改my.cnf

cd /usr/local/mysql/support-files

新建配置文件my.cnf

cp my.cnf /etc/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]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
# 一般配置选项
basedir = /usr/local/mysql
datadir = /soft/data/mysql
port = 3306
socket = /usr/local/mysql/mysqld.sock
character-set-server=utf8
[client]
port = 3306
socket = /usr/local/mysql/mysqld.sock
#下面是可选项,要不要都行,如果出现启动错误,则全部注释掉,保留最基本的配置选项,然后尝试添加某些配置项后启动,检测配置项是否有误
#back_log = 300
#max_connections = 3000
#max_connect_errors = 1000
#table_open_cache = 4096
#max_allowed_packet = 32M
#binlog_cache_size = 4M
#max_heap_table_size = 128M
#read_rnd_buffer_size = 16M
#sort_buffer_size = 16M
#join_buffer_size = 16M
#thread_cache_size = 16
#query_cache_size = 128M
#query_cache_limit = 4M
#ft_min_word_len = 8
#thread_stack = 512K
#transaction_isolation = REPEATABLE-READ
#tmp_table_size = 128M
#log-bin=mysql-bin
#long_query_time = 6
#server_id=1
#innodb_buffer_pool_size = 1G
#innodb_thread_concurrency = 16
#innodb_log_buffer_size = 16M
#innodb_log_file_size = 512M
#innodb_log_files_in_group = 3
#innodb_max_dirty_pages_pct = 90
#innodb_lock_wait_timeout = 120
#innodb_file_per_table = on
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

cp mysql.server /etc/init.d/mysql

# 修改/etc/init.d/mysql

vim /etc/init.d/mysql

basedir=/usr/local/mysql
datadir=/home/data/mysql
1
2

# 启动

/etc/init.d/mysql start

# 登录

mysql -h localhost -u root -p

如果出现-bash: mysql: command not found

执行ln -s /usr/local/mysql/bin/mysql /usr/bin

输入上面的临时密码

--修改密码
mysql> set password=password('密码');

--授权
mysql>grant all privileges on *.* to '用户'@'%' identified by '密码';
mysql>flush privileges;

--查看表
mysql> use mysql;
mysql> select host,user from user;
1
2
3
4
5
6
7
8
9
10

# 添加系统路径

vim /etc/profile

添加:

export PATH=/usr/local/mysql/bin:$PATH

重启:source /etc/profile

# 配置自启动

chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
1
2
3

# FAQ

# 1.mysql启动报错

​ The server quit without updating PID file (/usr/local/my (opens new window)sql (opens new window)/var/xxx.pid)

​ https://blog.csdn.net/zhou75771217/article/details/82893997

chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /home/data/mysql
1
2

# 2.断电导致innodb损坏无法启动

​ 尝试强制恢复

​ 在mysql的配置文件my.cnf里找到 [mysqld]字段下,添加 innodb_force_recovery=1:

​ vim /etc/my.cnf

[mysqld]
innodb_force_recovery = 1
1
2

​ 如果innodb_force_recovery = 1不生效,则可尝试2——6几个数字

​ 然后重启mysql,重启成功。然后使用mysqldump或 pma 导出数据,执行修复操作等。修复完成后,把该参数注释掉,还原默认值0。

# innodb_force_recovery参数解析

​ innodb_force_recovery影响整个InnoDB存储引擎的恢复状况。默认为0,表示当需要恢复时执行所有的恢复操作(即校验数据页/purge undo/insert buffer merge/rolling back&forward),当不能进行有效的恢复操作时,mysql有可能无法启动,并记录错误日志; innodb_force_recovery可以设置为1-6,大的数字包含前面所有数字的影响。当设置参数值大于0后,可以对表进行select,create,drop操作,但insert,update或者delete这类操作是不允许的。

  • 1(SRV_FORCE_IGNORE_CORRUPT):忽略检查到的corrupt页。
  • 2(SRV_FORCE_NO_BACKGROUND):阻止主线程的运行,如主线程需要执行full purge操作,会导致crash。
  • 3(SRV_FORCE_NO_TRX_UNDO):不执行事务回滚操作。
  • 4(SRV_FORCE_NO_IBUF_MERGE):不执行插入缓冲的合并操作。
  • 5(SRV_FORCE_NO_UNDO_LOG_SCAN):不查看重做日志,InnoDB存储引擎会将未提交的事务视为已提交。
  • 6(SRV_FORCE_NO_LOG_REDO):不执行前滚的操作。