centos7安装最新稳定GA版mysql-5.7.31实战全过程详解
ps:mysql最新GA版是5.7.31了,之前是5.7.28
1.查询数据库版本
mysqld -version
mysqld (mysqld 5.7.31)
2. 卸载自带的mariadb和mysql
# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
# rpm -qa | grep mysql
如果没有,就可以安装mysql,如果有,需要先卸载(remove后为上面命令查询到的内容,全文件名)
yum remove mariadb-libs-5.5.60-1.el7_5.x86_64
卸载完成提示Complete!即可
3.下载mysql文件
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
版本选择:5.7.31,系统选择:Linux - Generic,系统版本选择:Linux - Generic(glibc 2.12)(x86,64-bit)
选择Compressed TAR Archive (mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz) 下载
https://dev.mysql.com/downloads/file/?id=496462
下载页面不用登录或注册,点击
No thanks, just start my download.
进行下载即可,下载后上传到服务器/opt/mysql目录下进行安装
注:由于这个在国内下载速度很慢,上传速度也很慢
可以直接复制浏览器下载里面的url地址,在服务器上用wget命令直接下载
到/opt/mysql目录下(没有mysql目录则创建一个)执行:wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
4.安装数据库
进入/opt/mysql目录下解压
tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
解压完成后重命名
mv mysql-5.7.31-linux-glibc2.12-x86_64 mysql-5.7.31
检查mysql组和用户是否存在,如果没有则创建
# cat /etc/group|grep mysql
# groupadd mysql
# useradd -r -g mysql mysql
注:useradd -r 参数表示mysql用户是系统用户,不可用于登录系统
创建data目录
# cd /opt/mysql/mysql-5.7.31
# mkdir data
# ls
bin data docs include lib LICENSE man README share support-files
将/opt/mysql/mysql-5.7.31 的所有者及所属组改为mysql
# chown -R mysql:mysql /opt/mysql/mysql-5.7.31
在/opt/mysql/mysql-5.7.31/support-files目录下创建my_default.cnf
# cd /opt/mysql/mysql-5.7.31/support-files
# vim my_default.cnf
添加下面内容:
[mysqld]
#设置mysql的安装目录
basedir = /opt/mysql/mysql-5.7.31
#设置mysql数据库的数据存放目录
datadir = /opt/mysql/mysql-5.7.31/data
#设置端口
port = 3306
socket = /tmp/mysql.sock
#设置字符集
character-set-server=utf8
#日志存放目录
log-error = /opt/mysql/mysql-5.7.31/data/mysqld.log
pid-file = /opt/mysql/mysql-5.7.31/data/mysqld.pid
#允许时间类型的数据为零(去掉NO_ZERO_IN_DATE,NO_ZERO_DATE)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#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
保存退出
拷贝my_default.cnf文件到/etc/my.cnf 存在则覆盖
# cp my_default.cnf /etc/my.cnf
初始化Mysql
./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/mysql-5.7.31/ --datadir=/opt/mysql/mysql-5.7.31/data/
如果报错:./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
就安装libaio,安装命令:yum install libaio 如果没有则跳过
初始化完成之后查看日志
cat /opt/mysql/mysql-5.7.31/data/mysqld.log
最后一行里包含了root用户的临时密码如:[Note] A temporary password is generated for root@localhost: 6LkwFig+oi0R
配置环境变量,编辑/etc/profile,方便在任何地方用mysql命令
vim /etc/profile
在最后面添加下面内容:
#mysql
export MYSQL_HOME=/opt/mysql/mysql-5.7.31
export PATH=$PATH:$MYSQL_HOME/bin
重新编译profile让配置生效
source /etc/profile
测试方法输入:mysql -u root -p 提示输入密码则说明设置成功了。
设置开机自启动,把启动脚本放到开机初始化目录/etc/init.d/下,注意后面的是mysqld,不是mysql
cp /opt/mysql/mysql-5.7.31/support-files/mysql.server /etc/init.d/mysqld
给mysqld增加执行权限,否则无法正常自启动
chmod +x /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --list mysqld
测试方法是重启服务器reboot看看是否自动启动了,这个留最后进行。
如果重启服务器后,输入:mysql -u root -p 报错如下,说明没有启动mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
另外一种可以控制顺序的开机自启动设置方式:
# vim /etc/rc.local
添加systemctl start mysqld
先执行systemctl start mysqld
再执行需要依赖数据库的脚本xxx.sh(数据库没启动之前执行脚本会失败)
注:经过多次测试添加service mysqld start,systemctl start mysqld 都无法正常启动
----------------
启动mysql
# service mysql start
Starting MySQL.. SUCCESS!
进入mysql并更改密码
# cd /opt/mysql/mysql-5.7.31
# ./bin/mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# ./bin/mysql -u root -p6LkwFig+oi0R
mysql> set password=password('myrootPassword888');
mysql> grant all privileges on *.* to root@'%' identified by 'myrootPassword888';
mysql> flush privileges;
添加远程访问权限,如果update时报错(%是特殊字符),查询一下是否已更改,最后执行刷新。
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> select host,user from user where user = 'root';
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| localhost | root |
+-----------+------+
2 rows in set (0.00 sec)
mysql> flush privileges;
mysql> exit;
重启mysql生效
# service mysql restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
常用命令
service mysqld start #启动
service mysqld stop #关闭
service mysqld restart #重启
service mysqld status #查看运行状态