1.下载安装包
http://mirrors.sohu.com/mysql/MySQL-5.7/
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
2、解压到/opt下
tar xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
3.创建用户
groupadd mysql
useradd -g mysql -s /sbin/nologin -d /usr/local/mysql/ -M mysql
4.添加环境变量
echo PATH=$PATH:/usr/local/mysql/bin >>/etc/profile
source /etc/profile
5.创建必要的目录
mkdir -p /data/mysql/{data,tmp,logs}
6.添加配置文件
vi /etc/my.cnf
####配置文件内容#####
[client]
#客户端设置
port = 3306
socket = /data/mysql/data/mysql.sock
default-character-set = utf8mb4
[mysqld]
user = mysql
port = 3306
socket = /data/mysql/data/mysql.sock
server-id = 1
pid-file = /data/mysql/data/mysql.pid
#安装目录
basedir = /usr/local/mysql
#数据库存放目录
datadir = /data/mysql/data/
#系统数据库编码设置,排序规则
character_set_server = utf8mb4
collation_server = utf8mb4_bin
back_log = 1024
explicit_defaults_for_timestamp = ON
lower_case_table_names = 0
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
max_allowed_packet = 8M
thread_stack = 256K
thread_cache_size = 384
skip-external-locking
interactive_timeout = 600
wait_timeout = 3600
log_timestamps = SYSTEM
log-error = /data/mysql/logs/error.log
#默认使用InnoDB存储引擎
default_storage_engine = InnoDB
innodb_buffer_pool_size = 64M
innodb_purge_threads = 1
innodb_log_buffer_size = 2M
innodb_log_file_size = 128M
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 32M
myisam_sort_buffer_size = 8M
# MySQL重建索引时所允许的最大临时文件的大小
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
7.添加权限
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /usr/local/mysql
8.初始化数据库
yum install -y libaio
yum -y install numactl
mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/data/
9.查看初始密码
more /data/mysql/logs/error.log
10.启动数据库
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
#设置为开机启动
chkconfig --level 35 mysqld on
11.登录数据库并修改密码
[root@bogon support-files]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log
Copyright (c) 2000, 2018, 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.
root@bogon 21:57: [(none)]>
root@bogon 21:57: [(none)]> SET PASSWORD = PASSWORD('root');
#设置远程连接权限
root@bogon 21:57: [(none)]>update mysql.user set host = '%' where user ='root';
root@bogon 21:57: [(none)]> flush privileges;
```
12.重新登录数据库
[root@bogon support-files]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
root@bogon 22:01: [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
如果使用navicat连接报2003错误 关闭防火墙可以解决
iptables -F
service iptables save
chkconfig iptables off