1、下载MySQL源码二进制包
MySQL官网下载地址:https://downloads.mysql.com/archives/community/
[root@db-server ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
2、安装mysql所需的依赖
[root@db-server ~]# yum install -y gcc-c++ readline-devel zlib-devel bison cmake
3、解压mysql源码包,更改目录名称
[root@db-server ~]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@db-server ~]# mv /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64/ /usr/local/mysql/
4、创建用户组
[root@db-server ~]# groupadd mysql
[root@db-server ~]# useradd -g mysql -s /bin/nologin -M mysql
[root@db-server ~]# id mysql //查看用户是否创建成功
uid=500(mysql) gid=500(mysql) groups=500(mysql)
5、创建数据库目录和日志目录
[root@db-server ~]# mkdir /mysql /mysql/data /mysql/log
6、更改目录权限
[root@db-server ~]# chown -R mysql:mysql /usr/local/mysql /mysql
7、创建MySQL配置文件my.cnf
[root@db-server ~]# vi /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server_id=10
port = 3306
user = mysql
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/data
pid-file = /mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
# [innodb]
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:1024M:autoextend
##log
log_error = /mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /mysql/log/mysql-slow.log
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
8、初始化数据库
[root@db-server mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data
9、查看日志文件,读取数据库初始密码
[root@db-server mysql]# cat /mysql/log/mysql-error.log
2018-08-15T06:31:26.790588Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
100 200 300 400 500 600 700 800 900 1000
100 200 300 400 500
100 200 300 400 500
2018-08-15T06:31:35.868595Z 0 [Warning] InnoDB: New log files created, LSN=45791
2018-08-15T06:31:36.000802Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T06:31:36.379668Z 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: dc04c1e2-a054-11e8-b0c8-080027a8bc50.
2018-08-15T06:31:36.402324Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-15T06:31:36.418716Z 1 [Note] A temporary password is generated for root@localhost: rbQpyfaUh2!N
10、MySQL数据库加密
[root@db-server mysql]# bin/mysql_ssl_rsa_setup --datadir=/mysql/data
11、配置环境变量,并使其生效
[root@db-server mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@db-server mysql]# source /etc/profile
12、配置MySQL服务
[root@db-server mysql]# cp support-files/mysql.server /etc/init.d/mysql
13、启动数据库服务、进入到数据库中
[root@db-server mysql]# /etc/init.d/mysql start
Starting MySQL.. SUCCESS!
[root@db-server mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.17-log
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
14、重置数据库密码
mysql> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
15、配置远程连接
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
16、修改数据库字符编码
[root@db-server mysql]# vi /etc/my.cnf
[mysqld]
character-set-server=utf8
[root@db-server mysql]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@db-server mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.17-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 18 sec
Threads: 1 Questions: 6 Slow queries: 0 Opens: 110 Flush tables: 1 Open tables: 64 Queries per second avg: 0.333
--------------