CentOS下安装MySql5.7

本文主要介绍了在CentOS7的环境下安装并配置MySql5.7的过程。

1. 下载MySql

1.1 官网下载

官网 选择社区版即可!

1.2 wget下载

1
$ wget http://downloads.mysql.com/archives/mysql-5.7/mysql-5.7.18.tar.gz

2. 安装

2.1 解压到工作目录

1
2
3
4
5
$ tar -xzvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local
$ cd /usr/local
$ mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql
$ cd mysql
$ mkdir data

2.2 增加mysql用户组

1
2
3
$ groupadd mysql
$ useradd -r -g mysql -s /bin/false mysql
$ chown -R mysql:mysql ./

2.3 初始化

1
$ bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql

在这一步会生成一个初始密码,请做好记录!

1
$ bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

3. 配置

3.1 my.cnf拷贝到/etc/下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
lower_case_table_names=1
# 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
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# 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
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

3.2 拷贝mysql到服务

1
2
3
$ cd support-files
$ 修改support-files/mysql.server下的basedir和datadir
$ cp mysql.server /etc/init.d/mysqld

3.3 启动/关闭mysql服务

1
2
3
$ service mysqld start
$ service mysqld stop
$ service mysqld restart

4. 配置mysql

4.1 使用初始密码进入

1
2
$ cd /usr/local/mysql
$ bin/mysql -uroot -p

如果不想每次以这种方式进入数据库,可以配置环境变量,打开/etc/profile文件,加上:

1
export PATH=$PATH:/usr/local/mysql/bin

4.2 修改root密码

1
2
$ ALTER USER 'root'@'localhost' IDENTIFIED BY 'pwd';
$ flush privileges;

4.3 配置root远程链接

1
2
$ grant all privileges on *.* to root@'%' identified by 'pwd';
$ flush privileges;

4.4 增加mysql用户

1
2
3
4
$ use mysql;
$ insert into user(Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject ) values ('%','user',password('pwd'),'','','');
$ GRANT ALL ON *.* TO 'user'@'%';
$ flush privileges;

至此你就完成了MySql的安装与用户配置,剩下的就可以愉快的玩耍数据啦!