CentOS 7 下 MySQL 5.7 的安装与配置及数据目录
本文测试环境:
- CentOS 7 64-bit Minimal
- MySQL 5.7
配置 yum 源
在 https://dev.mysql.com/downloads/repo/yum/ 找到 yum 源 rpm 安装包
image安装 mysql 源
# 下载
shell> wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装 mysql 源
shell> yum localinstall mysql57-community-release-el7-11.noarch.rpm
用下面的命令检查 mysql 源是否安装成功
shell> yum repolist enabled | grep "mysql.*-community.*"
image
安装 MySQL
使用 yum install 命令安装
shell> yum install -y mysql-community-server
启动 MySQL 服务
在 CentOS 7 下,新的启动/关闭服务的命令是 systemctl start|stop
shell> systemctl start mysqld
用 systemctl status
查看 MySQL 状态
shell> systemctl status mysqld
image
设置开机启动
shell> systemctl enable mysqld
# 重载所有修改过的配置文件
shell> systemctl daemon-reload
修改 root 本地账户密码
mysql 安装完成之后,生成的默认密码在 /var/log/mysqld.log
文件中。使用 grep 命令找到日志中的密码。
shell> grep 'temporary password' /var/log/mysqld.log
image
首次通过初始密码登录后,使用以下命令修改密码
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
或者
mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
以后通过 update set 语句修改密码
mysql> use mysql;
mysql> update user set password=PASSWORD('MyNewPass5!') where user='root';
mysql> flush privileges;
注意:mysql 5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误。查看 MySQL官网密码详细策略
添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须添加一个允许远程连接的帐户。或者修改 root 为允许远程连接(不推荐)
添加一个允许远程连接的帐户
mysql> GRANT ALL PRIVILEGES ON *.* TO 'zhangsan'@'%' IDENTIFIED BY 'Zhangsan2018!' WITH GRANT OPTION;
修改 root 为允许远程连接(不推荐)
mysql> use mysql;
mysql> UPDATE user SET Host='%' WHERE User='root';
mysql> flush privileges;
设置默认编码为 utf8
mysql 安装后默认不支持中文,需要修改编码。
修改 /etc/my.cnf
配置文件,在相关节点(没有则自行添加)下添加编码配置,如下:
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启mysql服务,查询编码。可以看到已经改过来了
shell> systemctl restart mysqld
shell> mysql -uroot -p
mysql> show variables like 'character%';
image
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
修改数据库目录:
- 查看原目录 - 方式1
> mysqld --help --verbose | grep 'datadir' | tail -1
2019-11-05 10:48:03 0 [Warning] Could not increase number of max_open_files to more than 1024 (request: 4198)
2019-11-05 10:48:03 0 [Warning] Changed limits: max_open_files: 1024 max_connections: 151 (was 151) table_cache: 421 (was 2000)
2019-11-05 10:48:03 0 [Note] Plugin 'FEEDBACK' is disabled.
2019-11-05 10:48:03 0 [Warning] Could not open mysql.plugin table. Some options may be missing from the help text
datadir /var/lib/mysql/
查看原目录 - 方式2(需登录MySQL命令行中执行)
> sudo mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.2.27-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select @@datadir as dataPath from dual ;
+--------------+
| dataPath |
+--------------+
| /var/lib/mysql/ |
+--------------+
1 row in set (0.00 sec)
更改新位置目录为:/home/mysql
注意:如果你将目录设置为/home/xxx/mysql,可能引发 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test,你会查各种资料,最终涉及到SELinux权限问题,整个处理起来太过繁琐。教你排查问题:/home/xxx/mysql新位置mysql目录的递归上级目录不能包含非root权限组的权限,否则你就算是按照很多教程配置的数据目录,最后还是不能启动服务。最后一句:注意整个路径所有目录权限!!!!
- 停止服务
sudo systemctl stop mysqld
- 查看服务状态是否停止
sudo systemctl status mysqld
- 将现有数据库目录复制到新位置
sudo cp -a -R /var/lib/mysql /home
- 原目录备份
sudo mv /var/lib/mysql /var/lib/mysql.bak
修改/etc/my.cnf,在[mysqld]下新增datadir、socket
sudo vi /etc/my.cnf
内容如下:
GNU nano 2.3.1 文件: /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[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
#socket=/var/lib/mysql/mysql.sock
datadir=/home/mysql # 新增
socket=/home/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
[client]
default-character-set=utf8
socket=/home/mysql/mysql.sock # 新增
[mysql]
default-character-set=utf8
- 重启服务
systemctl restart mysqld