1. 安装前准备

注:本文主要介绍 CentOS 系统二进制安装 mysql-5.7.24 版本的安装步骤,其他5.7系列版本的安装过程类似。

卸载旧版MySQL并清除MySQL残留

查看rpm包
rpm -qa|grep mysql 若有可用rpm -e *.rpm --nodeps卸载

查找mysql残留包
有则删除,不然残留的配置等相关设置可能会导致各种ERROR,没有则跳过
find / -name mysql

安装相关依赖
yum -y install make gcc-c++ cmake bison-devel ncurses-devel numactl libaio

创建用户和用户组
groupadd mysqluseradd -s /sbin/nologin -g mysql -M mysql

2. 下载二进制安装包并解压

cd /usr/local/wget在线下载
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

解压安装包
tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

解压后为了方便后面操作可把解压后文件名修改为mysql
mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql

更改文件夹所属权限
chown -R mysql.mysql /usr/local/mysql/

3. 创建mysql相关目录

mkdir -p /data/mysql/{data,logs,tmp}

更改文件夹所属
chown -R mysql.mysql /data/mysql/

4. 创建mysql配置文件my.cnf

vi /etc/my.cnf

简单模板如下(可直接复制粘贴)

[client]
port            = 3306
socket          = /data/mysql/tmp/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
port = 3306            

socket = /data/mysql/tmp/mysql.sock
pid-file  = /data/mysql/tmp/mysqld.pid
tmpdir = /data/mysql/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096

#innodb  
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G

#logs
server-id = 1003306
log-error = /data/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 3
log-bin = /data/mysql/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /data/mysql/logs/relay-bin
relay-log-recovery = 1  
relay_log_purge = 1

5. 配置mysql.server

进入support-files文件夹
cd /usr/local/mysql/support-files

复制mysql.server到/etc/init.d/mysql
cp mysql.server /etc/init.d/mysql

修改目录位置
vi /etc/init.d/mysql

配置

basedir=/usr/local/mysql
datadir=/data/mysql/data

注册开机启动服务
chkconfig --add mysqlchkconfig --list

6. 添加环境变量

echo "PATH=$PATH:/usr/local/mysql/bin " >> /etc/profile

使环境变量生效
source /etc/profile

7. 初始化mysql

/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data临时密码保存在errlog中

获取临时密码
more /data/mysql/logs/error.log |grep password

8. 启动mysql服务

启动mysql服务
service mysql start

使用初始密码登录mysql
mysql -uroot -p使用步骤7中获得的密码即可
此时执行sql操作可能会报如下错误,这时就需要修改密码才能使用了。建议使用方法一
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

9. 修改密码

方法一:登录后修改
格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:mysql> set password for root@localhost = password('123');

方法二:登录后更新user表
使用mysql库:mysql> use mysql; 更新密码:mysql> update user set password=password('123') where user='root' and host='localhost'; 刷新:mysql> flush privileges;

方法三:不登录用mysqladmin
格式:mysqladmin -u用户名 -p旧密码 password 新密码 例子:mysqladmin -uroot -p123456 password 123

9. 设置远程访问

不设置远程访问的话此数据库只能本地访问哦

登录并进入mysql库
use mysql;

查看当前root用户访问权限并修改
select user,host from user; mysql> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
此时会看到当前root只允许localhost访问,我们需要把需要远程访问的用户的host改成%,就是可以使用任意地址进行访问。
执行:update user set host='%' where user='root'; 修改好host之后,我们要让它立即生效,还要执行这样一个语句:flush privileges;

操作完成之后,再用root远程访问就可以了。