1.前置准备

  • 卸载旧版MySQL

查看rpm包

rpm-qa|grep mysql 若有可用 rpm-e卸载

查找mysql残留包,有则删除,没有则忽略

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/file/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz# 解压安装包tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz# 解压后为了方便后面操作可把解压后文件名修改为mysqlmv mysql-5.7.23-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

  1. vi /etc/my.cnf

  2. # 简单模板如下:

  3. [client]

  4. port            = 3306

  5. socket          = /data/mysql/tmp/mysql.sock


  6. [mysqld]

  7. user = mysql

  8. basedir = /usr/local/mysql        

  9. datadir = /data/mysql/data  

  10. port = 3306              


  11. socket = /data/mysql/tmp/mysql.sock

  12. pid-file  = /data/mysql/tmp/mysqld.pid

  13. tmpdir = /data/mysql/tmp    

  14. skip_name_resolve = 1

  15. symbolic-links=0

  16. max_connections = 2000

  17. group_concat_max_len = 1024000

  18. sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

  19. lower_case_table_names = 1

  20. log_timestamps=SYSTEM

  21. character-set-server = utf8

  22. interactive_timeout = 1800  

  23. wait_timeout = 1800

  24. max_allowed_packet = 32M

  25. binlog_cache_size = 4M

  26. sort_buffer_size = 2M

  27. read_buffer_size = 4M

  28. join_buffer_size = 4M

  29. tmp_table_size = 96M

  30. max_heap_table_size = 96M

  31. max_length_for_sort_data = 8096


  32. #logs

  33. server-id = 1003306

  34. log-error = /data/mysql/logs/error.log

  35. slow_query_log = 1

  36. slow_query_log_file = /data/mysql/logs/slow.log

  37. long_query_time = 3

  38. log-bin = /data/mysql/logs/binlog

  39. binlog_format = row

  40. expire_logs_days = 15

  41. log_bin_trust_function_creators = 1

  42. relay-log = /data/mysql/logs/relay-bin

  43. relay-log-recovery = 1  

  44. relay_log_purge = 1  


  45. #innodb  

  46. innodb_file_per_table = 1

  47. innodb_log_buffer_size = 16M

  48. innodb_log_file_size = 256M

  49. innodb_log_files_in_group = 2

  50. innodb_io_capacity = 2000

  51. innodb_io_capacity_max = 4000

  52. innodb_flush_neighbors = 0

  53. innodb_flush_method = O_DIRECT

  54. innodb_autoinc_lock_mode = 2

  55. innodb_read_io_threads = 8

  56. innodb_write_io_threads = 8

  57. innodb_buffer_pool_size = 2G

5.配置mysql.server

  1. cd /usr/local/mysql/support-files

  2. cp mysql.server /etc/init.d/mysql

  3. vi /etc/init.d/mysql

  4. # 修改目录位置

  5. basedir=/usr/local/mysql

  6. datadir=/data/mysql/data


  7. # 注册开机启动服务

  8. chkconfig --add mysql

  9. chkconfig --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 -palter user 'root'@'localhost' identified by 'root';flush privileges;