服务器环境:1,首先卸载Centos7自带的Mariadb
CentOS-7.4-x86_64-DVD-1708
MySQL版本:
mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
# 查看系统自带的Mariadb
[root@CDH-141 ~]# rpm -qa|grep mariadb
mariadb-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 mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
4,下载mysql离线安装包tar文件 官网下载地址:https://dev.mysql.com/downloa...
版本选择,可以选择一下两种方式:
【离线安装MySQL】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]# ls
binfull-path-to-mysql-VERSION-OSincludelib64mysql-5.7.25-linux-glibc2.12-x86_64.tar.gzshare
etcgamesliblibexecsbinsrc
[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.sql
mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC
[root@localhost ~]# cd /usr/local/
[root@CDH-141 local]# ls
binfull-path-to-mysql-VERSION-OSincludelib64mysql-5.7.25-linux-glibc2.12-x86_64.tar.gzshare
etcgamesliblibexecmysql-5.7.25-linux-glibc2.12-x86_64sbinsrc
# 进入/usr/local下,修改为mysql
[root@CDH-141 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql
[root@CDH-141 local]# ls
binetcfull-path-to-mysql-VERSION-OSgamesincludeliblib64libexecmysqlmysql-5.7.25-linux-glibc2.12-x86_64.tar.gzsbinsharesrc
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 chararter
default-character-set=utf8[mysqld]
socket=/tmp/mysql.sock
# set mysql server port
port = 3306 # 默认是3306,如果这里发现3306已经被占用,可以更改
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=1024
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
# 设置MySQL对表名等不区分大小写
lower_case_table_names=1
max_allowed_packet=200M
explicit_defaults_for_timestamp=true
#阻止过多尝试失败的客户端以防止暴力破解密码的情况,与性能并无太大的关系
max_connect_errors=30
#此参数确定数据日志文件的大小,以M为单位,根据数据更新频率调整。
innodb_log_file_size=50
#指定大小的内存来缓冲数据和索引,最大可以把该值设置成物理内存的80%
innodb_buffer_pool_size=10G
key_buffer_size=16M
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 配置GROUP_CONCAT拼接的字符串的长度字节
group_concat_max_len = 102400[mysql.server]
user=mysql
basedir=/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 --initialize
2019-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]'.mysqld0:off1:off2:on3:on4:on5:on6: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]'.mysqld0:off1:off2:on3:on4:on5:on6:off
[root@CDH-141 mysql]# service mysqld status
MySQL 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/mysql
export 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:42
poc3u0mO_luv
[root@CDH-141 mysql]#
13,修改密码
[root@CDH-141 mysql]# mysql -uroot -p
Enter password: #此处填写上边获取到的初始密码‘poc3u0mO_luv’
Welcome to the MySQL monitor.Commands end with ;
or \g.
Your MySQL connection id is 2
Server 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> exit
Bye# 验证登录
[root@localhost mysql]# mysql -uroot -p111111@123
mysql: [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 5
Server 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 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>
14,添加远程访问权限
注意:当前的访问权限是直接给root用户添加的,如果在项目现场部署的时候,是不能使用root用户的;
此时是有两种方案的,分别做了整理:
## 方案1:直接给root用户增加远程访问权限
# 添加远程访问权限
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1Changed: 1Warnings: 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 mysql
Reading table information for completion of table and column names
You 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: 1Changed: 1Warnings: 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 restart
Shutting down MySQL..[OK]
Starting MySQL..[OK]
[root@CDH-141 mysql]# ## 重启命令2
systemctlstartmysql
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