CentOS 安装MySQL5.7 源码方式安装
MySQL rpm方式安装:https://www.cnblogs.com/deverz/p/9560403.html
1、卸载已经安装的MySQL
yum list installed mysql
rpm -qa | grep mysql
以上两个查到的都删除掉,删除的是mysql的,不要删除其他程序的mysql插件,
如果是升级MySQL,切记备份数据再删除,且安装前关闭mysql服务。
2、下载源码包
这里下载的是5.7版本:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
3、解压压缩包并mv到指定目录
tar -xvzf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.26-linux-glibc2.12-x86_64 /data0/mysql
4、添加用户及用户组,分配目录权限
groupadd mysql useradd mysql -g mysql cd /data0 chown -R mysql chgrp -R mysql
5、编辑my.cnf【以下是参考】
[mysqld] basedir=/data0/mysql datadir=/data0/mysql/data socket=/tmp/mysql.sock user=mysql character-set-server = utf8mb4 collation-server = utf8mb4_general_ci init_connect='SET NAMES utf8mb4' lower_case_table_names = 1 port = 3306 tmpdir = /tmp
关于MySQL5.7配置文件:https://www.cnblogs.com/deverz/p/10997561.html
6、初始化MySQL
cd /data0/mysql ./bin/mysqld --initialize --user=mysql --basedir=/data0/mysql --datadir=/data0/mysql/data 其中/data0/mysql/data/目录是新建的所属用户和组都是mysql 5.7的初始使用的是mysqld --initialize 之前的版本使用的是mysql_install_db 初始化后,会有几个Warning 最后一行会有个类似已下的输出内容,等于初始了一个root密码,需要用它登录: 2019-06-10T05:09:55.612961Z 1 [Note] A temporary password is generated for root@localhost: (rjLmqO-7rqB
7、启动MySQL服务
cd /data0/mysql/support-files/ ./mysql.server start
8、登录MySQL
初始化时MySQL提供了一个密码,要是没有记录的话, 在/root/.mysql_secret文件中也有临时密码可以使用。 cd /data0/mysql ./bin/mysql -uroot -p'临时密码' 登录成功设置root密码: set password=password('123456789'); 设置远程连接 GRANT ALL PRIVILEGES ON *.* TO root@'%' identified by '123456789'; flush privileges;
9、添加系统服务
cp /data0/mysql/support-files/mysql.server /etc/init.d/mysqld 此时就可以使用service命令: service mysqld stop service mysqld start 添加开机启动的话: chkconfig --add mysqld chkconfig --list mysqld
chkconfig mysqld on
10、在/usr/bin/下添加mysql快捷方式
ln -s /data0/mysql/bin/mysql /usr/bin/mysql 这样就能全局使用 mysql -uroot -pxxx
11、若是升级MySQL到5.7执行sql遇到以下错误:
MySQL升级导致的问题 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
解决方式:
mysql> SHOW VARIABLES LIKE '%sql_mode%';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) 看到ONLY_FULL_GROUP_BY这一项,这是5.7的新功能,和老版本兼容出现了问题。 解决:去掉它 select version(), @@sql_mode;SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); mysql> SHOW VARIABLES LIKE '%sql_mode%'; +---------------+------------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+------------------------------------------------------------------------------------------------------------------------+ | sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------+------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)