centos系统安装MySQL5.7.22

1、下载

官网链接: https://dev.mysql.com/downloads/mysql/5.7.html#downloads


在线下载:wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

2、解压

tar zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql

3、重命名

mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql  或用一个软连接ln -s mysql-5.7.22-linux-glibc2.12-x86_64 mysql 指向mysql

4、卸载系统自带mysql

查看:rpm -qa | grep mysql

卸载:rpm -e --nodeps softfullname

5、创建用户组和用户

创建用户组:groupadd mysql

创建用户:useradd -r -g mysql mysql

为了安全性,给mysql数据库创建专有用户,该用户只能访问mysql目录,不能访问系统其它目录

另外不建议直接用root初始化mysql,否则连接mysql时会报错:[ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

6、给mysql目录指定专有用户和用户组

首先创建data目录:

cd /usr/local/mysql

mkdir data

指定用户和用户组:

cd /usr/local

chown -R mysql mysql/

chgrp -R mysql mysql/

-R包含目录下所有和目录和文件

7、初始化mysql

cd /usr/local/mysql/bin

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US

记住生成的临时密码

如果忘记密码或者想重新初始化,可以先将mysql/data目录中文件删除,然后再执行初始化命令

8、配置my.cnf

从5.7.17后mysql就没有默认的my_default.cnf文件,需要手动创建

cd /etc

cat>>my.cnf

输入以下内容,ctrl+D退出

精简版:

[plain]  view plain  copy
  1. [mysqld]  
  2. basedir=/usr/local/mysql/  
  3. datadir=/usr/local/mysql/data/  

可选版:

[plain]  view plain  copy
  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html  
  3. # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the  
  4. # *** default location during install, and will be replaced if you  
  5. # *** upgrade to a newer version of MySQL.  
  6.   
  7. [mysqld]  
  8. # sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES   
  9.   
  10. # 一般配置选项  
  11. basedir = /usr/local/mysql/  
  12. datadir = /usr/local/mysql/data  
  13. port = 3306  
  14. character-set-server = utf8  
  15. explicit_defaults_for_timestamp = true  
  16. # socket = /var/run/mysqld/mysqld.sock  
  17.   
  18. #下面是可选项,要不要都行,如果出现启动错误,则全部注释掉,保留最基本的配置选项,然后尝试添加某些配置项后启动,检测配置项是否有误  
  19. back_log = 300  
  20. max_connections = 3000  
  21. max_connect_errors = 50  
  22. table_open_cache = 4096  
  23. max_allowed_packet = 32M  
  24. #binlog_cache_size = 4M  
  25.   
  26. max_heap_table_size = 128M  
  27. read_rnd_buffer_size = 16M  
  28. sort_buffer_size = 16M  
  29. join_buffer_size = 16M  
  30. thread_cache_size = 16  
  31. query_cache_size = 128M  
  32. query_cache_limit = 4M  
  33. ft_min_word_len = 8  
  34.   
  35. thread_stack = 512K  
  36. transaction_isolation = REPEATABLE-READ  
  37. tmp_table_size = 128M  
  38. #log-bin=mysql-bin  
  39. long_query_time = 6  
  40.   
  41. server_id=1  
  42.   
  43. innodb_buffer_pool_size = 1G  
  44. innodb_thread_concurrency = 16  
  45. innodb_log_buffer_size = 16M  
  46.   
  47. innodb_log_file_size = 512M  
  48. innodb_log_files_in_group = 3  
  49. innodb_max_dirty_pages_pct = 90  
  50. innodb_lock_wait_timeout = 120  
  51. innodb_file_per_table = on  
  52.   
  53. [mysqldump]  
  54. quick  
  55. max_allowed_packet = 32M  
  56.   
  57. [mysql]  
  58. no-auto-rehash  
  59. default-character-set=utf8  
  60. safe-updates  
  61.   
  62. [myisamchk]  
  63. key_buffer = 16M  
  64. sort_buffer_size = 16M  
  65. read_buffer = 8M  
  66. write_buffer = 8M  
  67.   
  68. [mysqlhotcopy]  
  69. interactive-timeout  
  70.   
  71. [mysqld_safe]  
  72. open-files-limit = 8192  
  73.   
  74. [client]  
  75. /bin/bash: Q: command not found  
  76. 9、启动

    cd /usr/local/mysql/bin

    启动:./mysqld_safe --user=mysql &

      10、设为开机启动

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

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

    vi /etc/init.d/mysql

    将mysql目录填上:

    basedir=/usr/local/mysql/

    datadir=/usr/local/mysql/data/

    授权:chmod +x /etc/init.d/mysql

    设为开机启动:chkconfig --add mysql

    11、service启动

    重启服务:service mysql restart

    停止服务:service mysql stop

    启动服务:service mysql start

    查看服务:service mysql status

    12、登录mysql修改密码授权远程登录

    cd /usr/local/mysql/bin

    登录:./mysql -u root -p    输入临时密码

    修改密码:set password=password("root");

    登录授权:grant all privileges on *.* to'root' @'%' identified by 'root';

    授权生效:flush privileges;

    可使用navicat或sqlyog等工具进行登录,注意关闭防火墙或开放3306端口

    防火墙设置:
  77. 因centos7默认使用firewalld防火墙,而不是iptables,卸载firewalld,再安装iptables

    步骤:

    卸载firewalld:yum remove firewalld

    安装iptables:yum install iptables-services

    查看防火墙状态:service iptables status

    停止防火墙:service iptables stop

    启动防火墙:service iptables start

    设为开机不启动:systemctl disable iptables.service

    设为开机启动:systemctl enable iptables.service

    如果要开启防火墙,则需要开放特定端口,例:开放3306端口

    编辑:vi /etc/sysconfig/iptables

    添加配置:-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

    重启防火墙使配置生效:systemctl restart iptables.service

    其它相关命令:

    查看防火墙规则:iptables -L

    清空防火墙规则:iptables -F

    保存使操作生效:/etc/sysconfig/iptables save

    停止防火墙:/etc/sysconfig/iptables stop

    本文参考https://blog.csdn.net/vipbupafeng/article/details/80271089







猜你喜欢

转载自blog.csdn.net/qq_23323321/article/details/80377517