服务器环境:
CentOS-7.4-x86_64-DVD-1708
MySQL版本:
mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
1,首先卸载Centos7自带的Mariadb
# 查看零碎自带的Mariadb[root@CDH-141 ~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载零碎自带的Mariadb(此时要依据理论状况去卸载)[root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
2,查看Msyql是否存在
# 查看mysql是否存在[root@CDH-141 ~]# rpm -qa | grep mysql[root@CDH-141 ~]#
3,查看用户和组是否存在
(1),先查看是否存在,如果不存在则创立
# 查看mysql组和用户是否存在,如无则创立[root@CDH-141 ~]# cat /etc/group | grep mysql[root@CDH-141 ~]# cat /etc/passwd | grep mysql
# 创立mysql用户组[root@CDH-141 ~]# groupadd mysql# 创立一个用户名为mysql的用户,并退出mysql用户组[root@CDH-141 ~]# useradd -g mysql mysql# 制订password 为111111[root@CDH-141 ~]# passwd mysqlChanging password for user mysql.New password:BAD PASSWORD: The password is a palindromeRetype new password:passwd: all authentication tokens updated successfully.
4,下载mysql离线安装包tar文件
官网下载地址:https://dev.mysql.com/downloa...
版本抉择,能够抉择一下两种形式:
1)应用Red Hat Enterprise Linux
Select Version:5.7.25
Select Operating System:Red Hat Enterprise Linux / Oracle Linux
Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
列表中下载:
Compressed TAR Archive:(mysql-5.7.25-el7-x86_64.tar.gz)
2)应用Linux - Generic
Select Version:5.7.25
Select Operating System:Linux - Generic
Select OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)
列表中下载:
Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中应用的是这个版本】
留神:上边两种形式找mysql离线安装包的形式都能够。
5,上传第四步下载的mysql TAR包
# 上传mysql TAR包# 或者间接应用XFTP进行装置[root@CDH-141 ~]# rz# 解压mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz[root@CDH-141 local]# lsbin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz shareetc games lib libexec sbin src[root@CDH-141 local]# tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql-5.7.25-lin...mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sqlmysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sqlmysql-5.7.25-linux-glibc2.12-x86_64/support-files/magicmysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.servermysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BINmysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC[root@localhost ~]# cd /usr/local/[root@CDH-141 local]# lsbin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz shareetc games lib libexec mysql-5.7.25-linux-glibc2.12-x86_64 sbin src# 进入/usr/local下,批改为mysql[root@CDH-141 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql[root@CDH-141 local]# lsbin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz sbin share src
6,更改所属的组和用户
# 更改所属的组和用户[root@CDH-141 ~]# cd /usr/local/[root@CDH-141 local]# chown -R mysql mysql/[root@CDH-141 local]# chgrp -R mysql mysql/[root@CDH-141 local]# cd mysql/[root@CDH-141 mysql]# mkdir data[root@CDH-141 mysql]# chown -R mysql:mysql data
7,在/etc下创立my.cnf文件
# 编辑/etc/my.cnf[root@CDH-141 mysql]# vi /etc/my.cnf[mysql]socket=/tmp/mysql.sock# set mysql client default chararterdefault-character-set=utf8[mysqld]socket=/tmp/mysql.sock# set mysql server port port = 3306 # 默认是3306,如果这里发现3306曾经被占用,能够更改# set mysql install base dirbasedir=/usr/local/mysql# set the data store dirdatadir=/usr/local/mysql/data# set the number of allow max connnectionmax_connections=1024# set server charactre default encodingcharacter-set-server=utf8# the storage enginedefault-storage-engine=INNODB# 设置MySQL对表名等不辨别大小写lower_case_table_names=1max_allowed_packet=200Mexplicit_defaults_for_timestamp=true#阻止过多尝试失败的客户端以避免暴力破解明码的状况,与性能并无太大的关系max_connect_errors=30#此参数确定数据日志文件的大小,以M为单位,依据数据更新频率调整。innodb_log_file_size=50#指定大小的内存来缓冲数据和索引,最大能够把该值设置成物理内存的80%innodb_buffer_pool_size=10Gkey_buffer_size=16Msql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION# 配置GROUP_CONCAT拼接的字符串的长度字节group_concat_max_len = 102400[mysql.server]user=mysqlbasedir=/usr/local/mysql# 能够防止出现插入中文报错;如果此时不失效,能够强制在创立表的时候指定应用utf8的编码集 [client]default-character-set = utf8
8,进入mysql文件夹,并装置mysql
# 进入mysql[root@CDH-141 local]# cd /usr/local/mysql# 装置mysql[root@CDH-141 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty:2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead# 设置文件以及目录的权限[root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
9,启动MySQL
# 启动mysql[root@CDH-141 mysql]# /etc/init.d/mysqld restart ERROR! MySQL server PID file could not be found!Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'. SUCCESS!
10,设置开机自启动
#设置开机启动[root@CDH-141 mysql]# chkconfig --level 35 mysqld on[root@CDH-141 mysql]# chkconfig --list mysqldNote: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'.mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@CDH-141 mysql]# chkconfig --add mysqld[root@CDH-141 mysql]# chkconfig --list mysqldNote: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'.mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@CDH-141 mysql]# service mysqld statusMySQL running (26122)[ OK ][root@CDH-141 mysql]#
11,批改环境变量配置文件
# 进入/etc/profile文件夹[root@CDH-141 mysql]# vi /etc/profile批改/etc/profile,在最初增加如下内容# 批改/etc/profile文件export MYSQL_HOME=/usr/local/mysqlexport PATH=$MYSQL_HOME/bin:$PATH# 使文件失效[root@CDH-141 mysql]# source /etc/profile
12,取得MySQL初始密码
# 每个人的明码是不一样的,随机生成的[root@CDH-141 mysql]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2019-03-08 17:40:42poc3u0mO_luv[root@CDH-141 mysql]#
13,批改明码
[root@CDH-141 mysql]# mysql -uroot -pEnter password: #此处填写上边获取到的初始密码‘poc3u0mO_luv’Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.25Copyright (c) 2000, 2015, 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.mysql> set PASSWORD = PASSWORD('111111@123');Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> exitBye# 验证登录[root@localhost mysql]# mysql -uroot -p111111@123mysql: [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 5Server version: 5.7.27 MySQL Community Server (GPL)Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
14,增加近程拜访权限
留神:以后的拜访权限是间接给root用户增加的,如果在我的项目现场部署的时候,是不能应用root用户的;
此时是有两种计划的,别离做了整顿:
## 计划1:间接给root用户减少近程拜访权限# 增加近程拜访权限mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set host='%' where user='root';Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select host,user from user;+-----------+---------------+| host | user |+-----------+---------------+| % | root || localhost | mysql.session || localhost | mysql.sys |+-----------+---------------+3 rows in set (0.00 sec)mysql>
--- 计划2:追加一个demo用户,而后给demo用户增加近程拜访的权限mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed-- 此时是给所有用户凋谢的,如果须要对不不同的用户开发须要再独自设置mysql> update user set host='%' where user='demo';Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select host,user from user;+-----------+---------------+| host | user |+-----------+---------------+| % | demo || localhost | mysql.session || localhost | mysql.sys |+-----------+---------------+3 rows in set (0.00 sec)
15,重启MySQL使其失效
做上述操作之后,是须要重新启动MySQL,能力使其失效
# 重启mysql[root@CDH-141 mysql]# /etc/init.d/mysqld restartShutting down MySQL..[ OK ]Starting MySQL..[ OK ][root@CDH-141 mysql]# ## 重启命令2systemctl start mysql
16,如果连贯失败,须要查看防火墙
# 第一步:敞开防火墙systemctl stop firewalld
##################################### 留神,在理论中先敞开,确定是应用开发端口的形式实现,还是间接移除,再执行对应的操作##### 第二步:此时有两种解决方案# 计划1:移除防火墙systemctl disable firewalld# 计划2:开发3306端口# 凋谢端口请依据理论拜访规定配置,这里只是单纯开启3306端口的拜访,默认全放行,在生产环境禁止凋谢所有IP拜访# (1) 向防火墙增加 mysql 端口: firewall-cmd --zone=public --add-port=3306/tcp --permanent# (2) 刷新防火墙规定: firewall-cmd --reload# (3) 验证端口,查问防火墙凋谢端口: firewall-cmd --zone=public --list-port