前言
MySQL安装
零. 参考:官方文档
一. 下载mysql二进制文件,进入页面: https://dev.mysql.com/downloads/mysql/
Select Operating System: Linux-Generic 并下载
二. 切换到root用户创建group和user
root@chenruiwen:/data/mysql# groupadd mysql
root@chenruiwen:/data/mysql# useradd -r -g mysql dba -s /bin/false mysql
三. 解压下载的Mysql,重命名
root@chenruiwen:/data/mysql# tar -xf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
root@chenruiwen:/data/mysql# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql-5.7.21
四. 赋予mysql操作权限
root@chenruiwen:/data/mysql/mysql-5.7.21# chown -R dba /data/mysql/mysql-5.7.21
root@chenruiwen:/data/mysql/mysql-5.7.21# chgrp -R mysql /data/mysql/mysql-5.7.21
root@chenruiwen:/data/mysql/mysql-5.7.21# mkdir mysql-files
root@chenruiwen:/data/mysql/mysql-5.7.21# chown dba:mysql mysql-files/
root@chenruiwen:/data/mysql/mysql-5.7.21# chmod 750 mysql-files
五. 初始化Mysql
root@chenruiwen:/data/mysql/mysql-5.7.21# bin/mysqld --initialize --user=dba
mysqld: Can't create directory '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
2018-04-08T15:25:22.856110Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-08T15:25:22.856195Z 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.
2018-04-08T15:25:22.856935Z 0 [ERROR] Aborting
root@chenruiwen:/data/mysql/mysql-5.7.21#
提示目录不存在,因为我的mysql目录是在/data/mysql/mysql-5.7.21
所以,要指定数据目录和basedir并初始化。
root@chenruiwen:/data/mysql/mysql-5.7.21# mkdir data
root@chenruiwen:/data/mysql/mysql-5.7.21# bin/mysqld --user=dba --basedir=/data/mysql/mysql-5.7.21 --datadir=/data/mysql/mysql-5.7.21/data/ --initialize-insecure
2018-04-28T03:12:52.827802Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-28T03:12:53.883717Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-04-28T03:12:54.084754Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-04-28T03:12:54.150087Z 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: 0aca257d-4a92-11e8-af3b-00163e089ff5.
2018-04-28T03:12:54.153185Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-04-28T03:12:54.153653Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
这里我使用了 –initialize-insecure 参数,不设置root密码。
如果是 –initialize ,会随机生成一个密码。
六. 开启SSL,生成密钥文件
root@chenruiwen:/data/mysql/mysql-5.7.21# bin/mysql_ssl_rsa_setup
2018-04-28 11:21:57 [ERROR] Failed to access directory pointed by --datadir. Please make sure that directory exists and is accessible by mysql_ssl_rsa_setup. Supplied value : /usr/local/mysql/data
报错,需要修改配置文件。修改 /etc/my.cnf –datadir 属性,创建 my.cnf
root@chenruiwen:/data/mysql/mysql-5.7.21# vi /etc/my.cnf
MySQL配置文件简单版:
[mysqld]
# GENERAL
character-set-server=utf8
symbolic-links=0
basedir=/data/mysql/mysql-5.7.21
datadir=/data/mysql/mysql-5.7.21/data
socket=/tmp/mysql.sock
user=dba
port=3306
pid-file=/data/mysql/mysql-5.7.21/data/mysql.pid
default_storage_engine=InnoDB
#LOGGING
log_error=/data/mysql/mysql-5.7.21/log/mysql-error.log
#OTHER
open_files_limit=65535
[client]
socket=/tmp/mysql.sock
port=3306
[mysqld_safe]
log-error=/data/mysql/mysql-5.7.21/log/mysql-error.log
pid-file=/data/mysql/mysql-5.7.21/data/mysql.pid
定义的日志目录和文件手工创建一下,然后重新运行:
root@chenruiwen:/data/mysql/mysql-5.7.21# bin/mysql_ssl_rsa_setup
Generating a 2048 bit RSA private key
............................................+++
........................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
....................................+++
.................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..............................................................................+++
..........................................+++
writing new private key to 'client-key.pem'
-----
root@chenruiwen:/data/mysql/mysql-5.7.21#
更多开启SSL相关请参考官方文档: Creating SSL and RSA Certificates and Keys 七. 安全启动MySQL服务
root@chenruiwen:/data/mysql/mysql-5.7.21# bin/mysqld_safe --user=dba &
查看MySQL服务进程
root@chenruiwen:/data/mysql/mysql-5.7.21# ps -ef|grep mysql
root 14899 11849 0 15:20 pts/0 00:00:00 /bin/sh bin/mysqld_safe --user=dba
dba 15135 14899 0 15:20 pts/0 00:00:00 /data/mysql/mysql-5.7.21/bin/mysqld --basedir=/data/mysql/mysql-5.7.21 --datadir=/data/mysql/mysql-5.7.21/data --plugin-dir=/data/mysql/mysql-5.7.21/lib/plugin --user=dba --log-error=/data/mysql/mysql-5.7.21/log/mysql-error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql-5.7.21/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 15201 11849 0 15:27 pts/0 00:00:00 grep --color=auto mysql
启动成功。
八. (可选项)加到系统服务
root@chenruiwen:/data/mysql/mysql-5.7.21# cp support-files/mysql.server /etc/init.d/mysql.server
修改 mysqld 文件里的 basedir,datadir, mysqld_pid_file_path属性
连接MySQL
root@chenruiwen:/data/mysql/mysql-5.7.21# mysql -uroot -p
The program 'mysql' can be found in the following packages:
* mysql-client-core-5.7
* mariadb-client-core-10.0
Try: apt install <selected package>
安装客户端
root@chenruiwen:/data/mysql/mysql-5.7.21# apt-get update
root@chenruiwen:/data/mysql/mysql-5.7.21# apt-get install mysql-client-core-5.7
安装完成后重新连接MySQL,输入密码时直接回车,登录成功。
修改 root 密码:
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where user = 'root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
退出,重启即可。