Linux 安装 MySQL(包含源码安装和 yum 安装)
在Linux
上面安装MySQL
本应该是很容易的一件事,但是有的时候不注意细节还是很容易“翻车”,出现一些预料不到的问题。本文在实践中做一个简单的记录。
# Yum 安装
- 卸载之前的 MySQL(如果有的话)
pkill -9 mysqld # 停止MySQL
rpm -qa | grep -i mysql # 列出现有的MySQL安装包
yum -y remove 包名 # 使用这个命令删除列出来的包名
rpm -ev 包名 # 如果yum删除不了,就用这个命令删
2
3
4
- 下载 MySQL 源
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
2
- 替换清华源
根据这个地址
https://mirror.tuna.tsinghua.edu.cn/help/centos/
设置清华大学的源,注意把gpgcheck=1
改为gpgcheck=0
, 否则安装会失败
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
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
- 执行安装
yum -y install mysql-server
- 启动服务
systemctl enable mysqld
systemctl start mysqld
2
警告
如果启动服务报错,
- 如果是 SELinux 问题,请参阅解决 Linux-Centos7 启动 Mysql 服务失败_编程渣-王爽-CSDN 博客 (opens new window)
datadir
不对
2020-12-25T05:54:16.053547Z 0 [Warning] Failed to open optimizer cost constant tables
2020-12-25T05:54:16.053696Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2020-12-25T05:54:16.053715Z 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
2020-12-25T05:54:16.053760Z 0 [ERROR] Aborting
2
3
4
5
6
修改/etc/my.conf
中的datadir=/var/lib/mysql/mysql
,根据实际修改
参阅:Can't open and lock privilege tables: Table 'mysql.user' doesn't exist - Stack Overflow (opens new window)
- 以原始密码登录
grep 'password' /var/log/mysqld.log # 从这里拿到初始化的密码
mysql -uroot -p # 登录
2
- 修改密码安全策略 在测试环境使用,密码可以弄简单点,生产建议需要设置复杂
set global validate_password_length=0; # 密码最小长度策略
set global validate_password_policy=0; # 密码强度检查等级策略,0/LOW、1/MEDIUM、2/STRONG
set password for 'root'@'localhost' = password('123456'); # 密码修改为真实实际密码
2
3
参考配置:
[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
#
# 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
datadir=/var/lib/mysql/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 编码
character_set_server=utf8
init_connect='SET NAMES utf8'
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
- 开启 mysql 远程连接(可选)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
2
- 放开服务器端口权限(如果选择上步)
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
2
# 源码安装
# Linux
[root@172 mysql]# uname -a
Linux 172.18.1.117 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
# 发行版本
[root@172 mysql]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
2
3
4
5
# MySQL 版本
# 安装完成之后才可以查询到
[root@172 mysql]# mysql --version
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
2
3
# 下载
点击此链接 (opens new window)选择适宜版本下载;
如图所示进行选择
或者使用wget
下载:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
# 解压
我一般习惯在工作目录新建temp
文件夹,然后存放这些临时文件。
pwd
/root/temp/
tar -xzvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
# 省略解压结果
……
2
3
4
5
# 创建安装目录并切换
# 创建文件夹
mkdir /usr/local/mysql
# 将解压文件移动到新建目录
mv mysql-5.7.26-linux-glibc2.12-x86_64/* /usr/local/mysql/
# 切换目录
cd /usr/local/mysql
2
3
4
5
6
# 检查并创建用户和用户组
[root@localhost local]# cat /etc/group | grep mysql
[root@localhost local]# cat /etc/passwd |grep mysql
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -r -g mysql mysql
2
3
4
# 创建数据存储目录并赋权
注意:忘记授权会出问题,各种各样的问题。我第一次安装的时候没有新建用户并赋予相应权限,导致安装出现
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/xxx.pid).
等各种问题。
mkdir data
# 用户赋权
chown -R mysql mysql/
chgrp -R mysql mysql/
2
3
4
# 安装并初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
--pid-file=/usr/local/mysql/data/mysql.pid --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
2
注意:
- 第一次安装时没有设置
--lc_messages_dir
和--lc_messages
出现以下报错,所以建议直接使用完整的命令,如果已经出现下面的报错。可以使用rm -rf /usr/local/mysql/data/*
将数据删除之后重新初始化;
2019-06-23T08:38:15.774307Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
- 此步骤如果出现
ERROR
错误一定要注意解决,不能继续操作,如:[ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
1 - 记住最后一行生成的随机密码,安装成功后第一次登录需要使用:
# 此处密码随机生成,注意记录 2019-06-23T08:52:37.416821Z 1 [Note] A temporary password is generated for root@localhost: PDRO-ab.a8jd
1
2 - 最容易出现这个错误:
Starting MySQL. ERROR! The server quit without updating PID file
,网上搜到的解决方案没有啥用,我本次安装走到这里没有出现这个错误,但是下面启动服务环节出现该报错,使用rm /etc/my.cnf -rf
居然有用,暂时不知道造成原因。
# 使用 mysqld_safe 启动服务
/usr/local/mysql/bin/mysqld_safe --user=mysql
2019-06-23T09:00:10.100530Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
2
根据提示修改配置或者新建log
日志存放目录
[root@172 mysql]# mkdir /var/log/mariadb
[root@172 mysql]# touch /var/log/mariadb/mariadb.log
2
再次启动
[root@172 mysql]# bin/mysqld_safe --user=mysql
2019-06-23T09:00:58.541328Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2019-06-23T09:00:58.546897Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
2
3
# 根据报错信息修改 my.cnf 中的配置项
[root@172 mysql]# vi /etc/my.cnf
# 此处省略,每个人的配置不一样(我已经将该文件删除了,没办法记住怎么配置的了,这里的错误信息很明确,根据错误修改到不报错即可)
[root@172 mysql]# bin/mysqld_safe --user=mysql
2019-06-23T09:03:54.795490Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2019-06-23T09:03:54.845647Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
2019-06-23T09:03:55.148856Z mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended
2
3
4
5
6
# 加入开机自启动项
将/usr/local/mysql/support-files/mysql.server
拷贝为/etc/init.d/mysql
并设置运行权限,这样就可以使用service mysql
命令启动/停止服务,
否则就只能使用/usr/local/mysql/bin/mysqld_safe
命令来启动服务
如果自定义了安装路径,还需要把mysql.server
中basedir
的相关路径,改为自定义的路径,默认路径是/usr/local/mysql
。本文使用默认路径,所以不用修改。
[root@dbserver support-files]# cp mysql.server /etc/init.d/mysql
[root@dbserver support-files]# chmod +x /etc/init.d/mysql
-- 把mysql注册为开机启动的服务
[root@dbserver support-files]# chkconfig --add mysql
-- 查看是否添加成功
[root@dbserver support-files]# chkconfig --list mysql
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 启动服务
# service mysql start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/172.18.1.117.pid).
2
此时删除配置文件,可以解决该报错(原因未知,没有深究)
[root@172 mysql]# rm /etc/my.cnf -rf
重新启动
[root@172 mysql]# /etc/init.d/mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/172.18.1.117.err'.
SUCCESS!
2
3
# 登录
[root@172 mysql]# /usr/local/mysql/bin/mysql -uroot -pPDRO-ab.a8jd
修改密码
# 有的文章说此处不能使用单引号,但是我使用单引号也成功了,好像没有问题。YOUR PASSWORD 为你自己设置的数据库的密码,这个要记住了,不然就要自己再去手动重置密码了!
mysql> set password=password("YOUR PASSWORD");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)
mysql> quit
Bye
2
3
4
5
6
7
# 添加环境变量
上一步中启动 mysql 的时候使用的是全路径,有的时候会比较麻烦,可以通过配置环境变量修改;
执行vi ~/.bash_profile
修改文件中PATH
一行,将/usr/local/mysql/bin
加入到PATH=$PATH:$HOME/bin
一行之后
注意:这种方法只对当前登录用户生效。