mysql官网下载mysql-5.7.29-el7-x86_64.tar.gz
[root@centos7 src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-el7-x86_64.tar.gz
一、卸载系统自带的Mariadb
[root@centos7 src]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@centos7 src]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
二、删除/etc下my.cnf配置文件(如果存在的话)、检查mysql是否存在
[root@centos7 src]# rm -rf /etc/my.cnf
[root@centos7 src]# rpm -qa | grep mysql
三、检查mysql用户组、用户是否存在,不存在则创建
[root@centos7 src]# cat /etc/group | grep mysql
[root@centos7 src]# cat /etc/passwd | grep mysql
[root@centos7 src]# groupadd mysql
[root@centos7 src]# useradd -g mysql mysql
四、解压mysql-5.7.29-el7-x86_64.tar.gz,移动到/usr/local/mysql5.7下,更改文件归属为mysql
[root@centos7 src]# tar -zxvf mysql-5.7.25-el7-x86_64.tar.gz
[root@centos7 src]# mkdir -p /usr/local/mysql5.7
[root@centos7 src]# mv /usr/local/src/mysql-5.7.25-el7-x86_64/* /usr/local/mysql5.7/
[root@centos7 src]# mkdir /usr/local/mysql5.7/data
[root@centos7 src]# mkdir /usr/local/mysql5.7/log
[root@centos7 src]# touch /usr/local/mysql5.7/log/mysqld.log
[root@centos7 src]# chown -R mysql /usr/local/mysql5.7/
[root@centos7 src]# chgrp -R mysql /usr/local/mysql5.7/
五、安装
[root@centos7 mysql5.7]# /usr/local/mysql5.7/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql5.7/ --datadir=/usr/local/mysql5.7/data/
会报如下错误:
原因:mysql 5.7不支持mysql_install_db,应该使用mysqld --initialize 完成实例初始化。
[root@centos7 mysql5.7]# /usr/local/mysql5.7/bin/mysqld --user=mysql --basedir=/usr/local/mysql5.7/ --datadir=/usr/local/mysql5.7/data/ --initialize
六、新建配置文件my.cnf,启动
[root@centos7 mysql5.7]# vi /etc/my.cnf
[root@centos7 mysql5.7]# chown 777 /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql5.7/
datadir=/usr/local/mysql5.7/data
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
lower_case_table_names=1
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
max_allowed_packet=16M
[client]
port=3306
[mysqld_safe]
log-error=/usr/local/mysql5.7/log/mysqld.log
设置开机启动:
[root@centos7 mysql5.7]# cp /usr/local/mysql5.7/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql5.7]# chmod +x /etc/init.d/mysqld
[root@centos7 mysql5.7]# service mysqld start
查看服务列表:
chkconfig --list
看到3、4、5状态为开或者为 on 则表示成功。如果是 关或者 off 则执行一下:chkconfig --level 345 mysqld on
七、添加环境变量
[root@centos7 ~]# vi /etc/profile
添加mysql环境变量
# MYSQL
export MYSQL_HOME="/usr/local/mysql5.7/"
export PATH="$PATH:$MYSQL_HOME/bin"
使环境变量生效
[root@centos7 ~]# source /etc/profile
八、获取初始密码,连接mysql,更改默认密码,允许远程访问
初始化密码在第5步执行初始化时有生成:
修改密码
[root@centos7 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29
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> set PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 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> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> create user 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
若连接不上,考虑防火墙是否关闭