说明:/usr/local/mysql地址可以自定义,这里演示默认/usr/local/mysql
1、下载tar包,这里使用wget从官网下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2、将mysql安装到/usr/local/mysql下
# 1.解压
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# 2.移动
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/
# 3.重命名
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
3、新建mysql用户、mysql用户组目录权限
#1. mysql用户组
groupadd mysql
# 2.mysql用户
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql
4、将/usr/local/mysql的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql
5.接下来初始化数据库
[root@host-10-5-0-131 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
6.复制配置文件到 /etc/my.cnf 这里是mysql的基本配置问题
命令:cp -a ./support-files/my-default.cnf /etc/my.cnf #会有一个选择是否覆盖原有文件,直接按 y回车选择是就可以
标注:mysql5.7 support-files下没有 my-default.cnf ,需要自己创建
7.把mysql放到本地系统服务中
1.把mysql放到本地系统服务中
[root@host-10-5-0-131 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
2.把mysqlld授权
[root@host-10-5-0-131 mysql]# chmod a+x /etc/init.d/mysqld
3.添加到开发服务
[root@host-10-5-0-131 mysql]# chkconfig --add /etc/init.d/mysqld
4.设置开机启动
[root@host-10-5-0-131 mysql]# chkconfig mysqld on
5.启动服务
[root@host-10-5-0-131 mysql]# service mysqld start
8.【可选项】配置mysql环境变量
echo "export PATH=$PATH:/usr/local//mysql/bin">> /etc/profile
source /etc/profile
9.修改初始化密码
当操作数据库命令的时候会提示该错误(先改密码才能操作):
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
1.改密码(登录后):
mysql> alter user root@localhost identified by '新密码';
Query OK, 0 rows affected (0.00 sec)
2.改密码(未登录)不建议,密码明文暴露:
mysqladmin -uroot -p'旧密码' password'新密码'
PS:到这数据已经按照完成,以下是可选或可配置的选项
1.【可选】重新初始化(一般出问题才重新初始化)
2.设置允许所有远程客户端连接登录
1..mysql>use mysql;
2.mysql> select host,user,authentication_string from user;
3.mysql> update user set host='%' where user='root' and host='localhost';
4.mysql>flush privileges; //修改生效,一定不能落下这步,否则第三步不生效
5.mysql> select host,user,authentication_string from user;
重新查看一遍,如图已经修改完成
6.查看开放的端口是否有3306:firewall-cmd --list-ports
7.没开启3306的话就开启防火墙端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent
注意:添加端口后,必须用命令firewall-cmd --reload重新加载一遍才会生效
这样远程客户端连接就可以了。。。