适用
1、为解决Mysql漏洞而需要离线升级mysql版本;
2、初次安装Mysql也可以参考本文;
安装
提示:首次安装可跳过第二步、第三步
一、查看CentOS7系统自带mariadb
# 查看系统自带的Mariadb
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载系统自带的Mariadb
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rm /etc/my.cnf
二、检查mysql是否存在
# 检查mysql是否存在
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa | grep mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]#
三、查看用户和组是否存在
1)检查mysql组合用户是否存在
# 检查mysql组和用户是否存在,如无则创建
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/group | grep mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd | grep mysql
# 查询全部用户(只是做记录,没必要执行)
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more
root|010
sync|510
mysql|99711001
2)若不存在,则创建mysql组和用户
# 创建mysql用户组
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# useradd -g mysql mysql
# 制定password 为111111
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# 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.
四、下载mysql的tar包 提示:下载之前需查看操作系统,不要下载错了数据库版本
访问地址:https://dev.mysql.com/downloads/mysql/
五、上传第四步下载的mysql TAR包到
# 进入/usr/local/src文件夹
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cd /usr/local/src/
# 上传mysql TAR包
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# rz
# 解压tar文件
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# tar xvf mysql-5.7.22-el7-x86_64.tar
# 解压mysql-5.7.22-el7-x86_64.tar.gz
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# tar -zxvf mysql-5.7.22-el7-x86_64.tar.gz
# 解压后的文件移动到/usr/local文件夹
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# mv mysql-5.7.22-el7-x86_64 /usr/local
# 进入/usr/local下,修改为mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# cd /usr/local
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# mv mysql-5.7.22-el7-x86_64 mysql
六、更改所属的组和用户
# 更改所属的组和用户
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chown -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chgrp -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# cd mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# mkdir data
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown -R mysql:mysql data
七、在/etc下创建my.cnf文件
# 进入/etc文件夹下
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cd /etc
# 创建my.cnf文件
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# touch my.cnf
# 编辑my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# vim my.cnf
1)my.cnf添加如下内容:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
2)查看my.cnf内容
# 查看my.cnf文件
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cat /etc/my.cnf
八、进入mysql文件夹,并安装mysql
# 进入mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# cd /usr/local/mysql/
# 安装mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-07-04 15:46:02 [WARNING] 5mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-04 15:46:05 [WARNING] The bootstrap log isn't empty:
2018-07-04 15:46:05 [WARNING] 2018-07-04T15:46:02.728710Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-07-01T15:46:02.729161Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-07-04 T15:46:02.729167Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown 777 /etc/my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod +x /etc/init.d/mysqld
九、启动mysql
# 启动mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# /etc/init.d/mysqld restart
MySQL manager or server PID file could not be found! [FAILED]
解决:
# 2、杀死进程
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# kill -9 10031
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# kill -9 10220
# 3、重启mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.
十、设置开机启动
#设置开机启动
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --level 35 mysqld on
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --add mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# service mysqld status
SUCCESS! MySQL running (4475)
十一、修改配置文件
# 进入/etc/profile文件夹
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# vim /etc/profile
1)修改/etc/profile,在最后添加如下内容
# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# source /etc/profile
十二、获得mysql初始密码
# 1、获得mysql初始密码
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2017-04-17 17:40:02
_pB*3VZl5T<6
# 2、修改密码
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
十三、添加远程访问权限
# 添加远程访问权限
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 -A
Database changed
mysql> update user set host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
十四、重启mysql生效
# 重启mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.
备注:
由于安装在/usr/local下面的mysql,因此可以在热河文件夹启动mysql
若安装在别的文件夹,请执行以下命令:
# 为了在任何目录下可以登录mysql
ln -s /你的mysql路径/mysql /usr/local/mysql
遇到问题
1、在获得mysql初始密码时请复制粘贴勿手打
2、无法同时创建’root’@‘%’、‘root’@‘localhost’
drop user 'root'@'%';
drop user 'root'@'localhost';
flush privileges;
create user 'root'@'%' identified by '密码';
create user 'root'@'localhost' identified by '密码';
select user,host from user;
3、mysql “select command denied to user root” 问题解决
找到mysql数据库下的user表,把对应的用户权限修改为’Y’
use mysql
update user set Select_priv='Y' where user='root' and host='%';
select host,user,Select_priv from user;
4、启动之后报错
j问题:ava.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘catalog_nanchang.system1.region’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by文章来源:https://www.toymoban.com/news/detail-486442.html
解决:
1.打开MySQL的配置文件。在Linux系统上,MySQL的配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf。
2.在配置文件中找到[mysqld]部分,这是MySQL服务器的配置部分。文章来源地址https://www.toymoban.com/news/detail-486442.html
#在[mysqld]部分中添加或修改以下行:
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
到了这里,关于linux 漏洞升级、初次安装 mysql的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!