安装MySQL
# 安装MySQL
# 1、下载mysql5.7版本
这里用的是数据库5.7版本的,有的可以用8+版本的,基本上安装的步骤是一样的,就版本的差别,这里以5.7为例进行安装。
参考文章:https://www.modb.pro/db/72437 (opens new window)
# 2、检查MySQL安装
- 这是未安装的按情况。
[root@Captian /]# rpm -qa | grep mysql
[root@Captian /]#
2
如果存在的话最好先删除,否则后序版本不一致会有问题
例如:可以通过以下命令删除:最后面的就是你查询出来的 mysql的信息
rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
- 是否有残留的mysql文件夹
主要通过以下两个命令查询:
[root@Captian /]# whereis mysql
[root@Captian lib]# find / -name mysql
2
存在这样的文件的话,我们就需要将这些文件删除:
[root@Captian /]# rm -rf /usr/lib64/mysql /usr/share/mysql /etc/selinux/targeted/active/modules/100/mysql
验证是否已经删除干净了:
[root@Captian /]# whereis mysql
[root@Captian lib]# find / -name mysql
2
如果出现 find: ‘/run/user/1000/gvfs’: 权限不够
只需要输入下面的命令删除该文件即可
umount /run/user/1000/gvfs // 卸载该文件
rm -rf /run/user/1000/gvfs // 删除该文件
2
这样之后查询文件就不会出现这条提示信息了
- 检查mysql用户组和用户是否存在,如果没有,则创建
[root@Captian /]# cat /etc/group | grep mysql
[root@Captian /]# cat /etc/passwd |grep mysql
[root@Captian /]# groupadd mysql
[root@Captian /]# useradd -r -g mysql mysql
[root@Captian /]#
2
3
4
5
# 3、安装mysql
先在local目录下新建一个cap文件夹
。
下载完成之后将压缩包上传到 /usr/local/cap
路径下
- 解压文件并重命名
这里我们先通过以下的命令切换到压缩包所在的路径下:
cd /usr/local/cap
然后我们将文件先移动/usr/local/
下,然后再解压。
mv mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz /usr/local/
tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2
之后我们将解压后的文件夹重命名为mysql
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
之后我们通过 ll 命令可以看到文件已经改名:
[root@Captian local]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
[root@Captian local]# ll
total 629808
drwxr-xr-x. 2 root root 4096 May 11 2019 bin
drwxr-xr-x. 2 root root 4096 May 11 2019 etc
drwxr-xr-x. 2 root root 4096 May 11 2019 games
drwxr-xr-x. 2 root root 4096 May 11 2019 include
drwxr-xr-x. 2 root root 4096 May 11 2019 lib
drwxr-xr-x. 2 root root 4096 May 11 2019 lib64
drwxr-xr-x. 2 root root 4096 May 11 2019 libexec
drwxr-xr-x 9 root root 4096 Jul 20 11:20 mysql
-rw-r--r-- 1 root root 644869837 Jul 20 11:15 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x. 2 root root 4096 May 11 2019 sbin
drwxr-xr-x. 5 root root 4096 Jan 26 2021 share
drwxr-xr-x. 2 root root 4096 May 11 2019 src
drwxr-xr-x 2 root root 4096 Jul 20 11:18 xyh
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 创建数据库目录并且赋予权限
mkdir -p /data/mysql #创建目录
chown mysql:mysql -R /data/mysql #赋予权限
2
- 修改my.cnf文件
vim /etc/my.cnf
之后我们点击 i 键即可开始编辑,添加下面的内容:
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
2
3
4
5
6
7
8
9
10
11
12
13
# 4、初始化数据库
这里我们需要先进入mysql 的 bin 目录下
cd /usr/local/mysql/bin
进入之后我们输入以下命令初始化数据库:
[root@Captian bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
[root@Captian bin]#
2
之后我们需要查看以下我们的密码:
cat /data/mysql/mysql.err
[root@Captian /]# cat /data/mysql/mysql.err
2022-07-20T03:25:04.025470Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-20T03:25:04.155758Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-20T03:25:04.219645Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8bbfeba7-07db-11ed-8664-fa202023ca79.
2022-07-20T03:25:04.221350Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-20T03:25:04.221723Z 1 [Note] A temporary password is generated for root@localhost: rK.)YfuIh2Js
2
3
4
5
6
7
这个就是登录的初始密码:rK.)YfuIh2Js
# 5、启动数据库并更改密码
# 5.1、放配置文件
先将mysql.server
放置到 etc/init.d/mysql
中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动mysql
service mysql start
然后查看是否启动成功
ps -ef|grep mysql
看到下面的页面,就说明我们已经成功安装mysql了:
[root@Captian bin]# service mysql start
Starting MySQL. [ OK ]
2
# 5.2、修改密码
之后我们就只需要登录mysql,修改密码即可.
./mysql -u root -p #bin目录下
[root@Captian bin]# ./mysql -u root -p
Enter password:
2
然后输入我们刚才那个随机生成的密码,进入下面的界面:
[root@Captian bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26
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.
2
3
4
5
6
7
8
9
10
11
12
13
现在我们就需要修改我们的登录密码了:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
2
3
4
5
然后exit,再重新登录一下,使用新的密码。
# 5.3、创建用户
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
# 6、开放端口连接Navicat
之后为了使得Navicat能够连接我们的数据库还要做以下的操作:
use mysql #访问mysql库
update user set host = '%' where user = 'root'; #使root能再任何host访问
FLUSH PRIVILEGES; #刷新
2
3
4
5
演示:
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> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
2
3
4
5
6
7
8
9
10
11
12
这些操作完之后我们在退出mysql之后还需要去打开服务器的3306端口,否则我们还是连接不上的
firewall-cmd --zone=public --add-port=3306/tcp --permanent;#开放端口
firewall-cmd --reload; #重启firewall
firewall-cmd --list-ports; #查看已经开放的端口
2
3
演示:
[root@Captian /]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@Captian /]# systemctl restart firewalld.service
[root@Captian /]# firewall-cmd --list-ports;
8080/tcp 3306/tcp
2
3
4
5
到这里我们就能够正常连接到Navicat上了:
# 7、问题解决
# 7.1、问题1
这里会有报错:2059 - Authentication plugin "caching. sha2 password' cannot be loaded解决方法
1、mysql用root用户登录 2、use mysql; 3、执行下列语句:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #修改加密规则
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #更新一下用户的密码
FLUSH PRIVILEGES; #刷新权限
2
3
4
5
localhost为本机ip,如果要所有ip连接,改为%:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; #修改加密规则
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #更新一下用户的密码
FLUSH PRIVILEGES; #刷新权限
2
3
4
5
# 7.2、问题2
Navicat pernium连接上Mysql后只能看到
information_schema
数据库是因为Mysql的root用户的没有权限。
使用命令show grants for root;
可以显示root用户的权限。
如果没有任何回执,则表明root用户根本没有权限。
使用命令GRANT ALL PRIVILEGES ON *.* TO root;
让root用户获得所有权限,再打开Navicat后就可以看见所有的数据库。
# 8、设置开机自启
如果mysql的安装目录名就是mysql,可以不用管此处配置
如果mysql的安装目录名是别的,如(mysql-5.7.26),此时需要修改mysql.server文件,否知开机启动不能使用
编辑mysql.server
vim /usr/local/mysql-5.7.26/support-files/mysql.server
1、将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql-5.7.35/support-files/mysql.server /etc/init.d/mysql
2、赋予可执行权限
chmod +x /etc/init.d/mysql
3、添加服务
chkconfig --add mysql
4、显示服务列表
chkconfig --list
如果看到mysql的服务,并且3,4,5都是开的话则成功,如果是关,则键入
chkconfig --level 345 mysql on
5、重启电脑
reboot
6、验证
netstat -na | grep 3306