一、安装步骤
MySQL下载官方网址:https://dev.mysql.com/downloads/mysql/
- 版本选择
选择Generic版本 - 下载MySQL
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
- 解压
tar -zxf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
- 移动并重命名
mv mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz /usr/local/mysql
二、配置
- 设置环境变量
[root@db01 ~]# vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
[root@db01 ~]# source /etc/profile
- 创建mysql用户
useradd mysql
- 创建相关目录并授权
mkdir -p /data/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
- 初始化数据库
初始化方法一
[root@db01 ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2019-09-21T11:10:23.024138Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-21T11:10:24.321756Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-09-21T11:10:24.502952Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-09-21T11:10:24.567784Z 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: 68db32d2-dc60-11e9-a7ac-fa163eeb8fda.
2019-09-21T11:10:24.569709Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-09-21T11:10:24.570070Z 1 [Note] A temporary password is generated for root@localhost: 2iNfDjsiww+V
新特性重要说明
5.7开始,MySQL加入了全新的密码安全机制
1. 初始化完成后,会生成临时密码(显示到屏幕上,并且会往日志中记一份)
2. 密码复杂度: 长度超过12位
3. 密码过期时间180天
初始化方法二: 不加安全机制,关闭上述特性
rm -rf /data/mysql/*
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2019-09-21T12:04:20.024078Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-21T12:04:21.322044Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-09-21T12:04:21.502184Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-09-21T12:04:21.567685Z 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: f2422f73-dc67-11e9-834b-fa163eeb8fda.
2019-09-21T12:04:21.570072Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-09-21T12:04:21.570367Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
- 编辑配置文件
vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=3306 [\\d]>
三、服务管理
3.1 使用service管理
[root@db01 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
3.2 使用systemd管理
- 编辑配置文件
[root@db01 ~]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
- 重新加载系统服务
[root@db01 ~]# systemctl daemon-reload
- 启动mysql
systemctl start mysqld
- 修改root密码
[root@db01 ~]# mysqladmin -uroot -p password 123
- 登录
[root@db01 ~]# mysql -uroot -p