MySQL5.7下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-el7-x86_64.tar.gz
1. 删除系统自带的Mariadb 或MySQL
#卸载系统自带的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
#删除etc目录下的my.cnf文件,若不存在可以略过此步
[root@localhost soft]# rm /etc/my.cnf
rm: 无法删除"/etc/my.cnf": 没有那个文件或目录
#检查mysql是否存在
[root@localhost ~]# rpm -qa | grep mysql
#检查mysql组和用户是否存在,如无创建
[root@localhost ~]# cat /etc/group | grep mysql
[root@localhost ~]# cat /etc/passwd | grep mysql
2. 新建mysql用户、用户组
#创建mysql用户组
[root@localhost ~]# groupadd mysql
#创建一个用户名为mysql的用户并加入mysql用户组(第一个mysql表示用户组,第二个表示用户名)
[root@localhost ~]# useradd -g mysql mysql
#设定password(mysql为我创建的用户名,可以自行调整)
[root@localhost soft]# passwd mysql
更改用户 mysql 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
3. 新建数据目录
#在home下新建data数据存放目录
[root@localhost ~]# cd /home
[root@localhost home]# mkdir mysqldata
#授权用户、组
[root@localhost home]# chown -R mysql:mysql mysqldata
4. 修改配置文件
在etc下新建配置文件my.cnf
my.cnf参考配置:
[mysql]
default-character-set=utf8
[mysqld]
skip-name-resolve
port = 3306
#此处自行调整,mysql的安装目录
basedir=/usr/local/mysql
#此处自行调整,mysql数据存放目录
datadir=/home/mysqldata
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
#log-bin=/data/mysql/mysql57/binlog
log-bin=OFF
binlog-format=ROW
server-id=1
#是否开始验证,若放开,则登录mysql时忽略验证
#skip-grant-tables
设置权限:
[root@localhost local]# chown 777 /etc/my.cnf
5.解压文件
将下载好的压缩文件上传到服务器,具体目录可以自己设置,我这里再home目录下新建了soft目录来存放
#进入压缩文件所在目录
[root@localhost soft]# pwd
/home/soft
#解压到/usr/local/
[root@localhost soft]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost soft]# cd /usr/local/
#重命名为mysql
[root@localhost local]# mv mysql-5.7.21-linux-glibc2.12-x86_64/ mysql
6.修改mysql所属用户组,初始化mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost local]# cd mysql/bin
[root@localhost bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/home/mysqldata/
### 如果上面命令初始化不成功可以使用下面语句初始化
[root@localhost bin]# ./mysql_install_db --no-defaults --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysqldata/
2019-10-18 09:17:08 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-10-18 09:17:12 [WARNING] The bootstrap log isn't empty:
2019-10-18 09:17:12 [WARNING] 2019-10-18T01:17:08.767197Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2019-10-18T01:17:08.767839Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5010)
2019-10-18T01:17:08.767845Z 0 [Warning] Changed limits: max_connections: 214 (requested 1000)
2019-10-18T01:17:08.767847Z 0 [Warning] Changed limits: table_open_cache: 400 (requested 2000)
7.防火墙设置
#查看防火墙状态是否启动 ,如下是已经启动
[root@localhost bin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2019-10-15 16:20:16 CST; 2 days ago
Docs: man:firewalld(1)
Main PID: 1223 (firewalld)
CGroup: /system.slice/firewalld.service
└─1223 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
10月 15 16:20:15 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
10月 15 16:20:16 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
#如果防火墙未启动, 通过systemctl start firewalld开启防火墙,没有任何提示即开启成功。
[root@localhost bin]# systemctl start firewalld
#添加端口
[root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
#重新加载设置
[root@localhost bin]# firewall-cmd --reload
success
8.设置全局查看mysql服务
#如果未配置会出现如下
[root@localhost support-files]# service mysql status
Redirecting to /bin/systemctl status mysql.service
Unit mysql.service could not be found.
#配置
[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@localhost support-files]# vi /etc/init.d/mysql
#修改内容,具体目录可以自行调整
basedir=/usr/local/mysql
datadir=/home/mysqldata
#修改权限
[root@localhost support-files]# chmod +x /etc/init.d/mysql
[root@localhost init.d]# service mysql status
ERROR! MySQL is not running
9. mysql用户初始化默认密码、远程连接初始化
#查看mysql服务状态
[root@localhost bin]# service mysql status
ERROR! MySQL is not running
#启动,stop是停止
[root@localhost bin]# service mysql start
Starting MySQL. SUCCESS!
[root@localhost bin]# service mysql status
SUCCESS! MySQL running (7645)
##获取默认密码
[root@localhost bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2019-10-18 09:17:08
1fNsl6LkQ+*/
#登录
[root@localhost bin]# 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.21
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('123456a');
###切库
mysql>use mysql;
###若修改密码不生效,使用以下命令修改
mysql>update user set authentication_string=PASSWORD("自定义密码") where user='root';
##修改权限,设置远程访问,user后的单引号是用户名
mysql>update user set host='%' where user='root';
###刷新
mysql> flush privileges;
如果修改无效,刷新一下再重启mysql服务!
至此,结束,可以使用navicat远程连接上!
附带一些基本命令
show databases;//显示所有数据库
show tables;//显示所有表
use 库名;//切库
desc 表名;//查看表结构