MySQL简单安装
1.下载安装包
本人比较习惯使用解压版安装:
大家可以在官网去选择自己的版本下载,可以使用wget直接下载到服务器目录;
官网地址
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-el7-x86_64.tar.gz
2.解压安装包
tar -xzvf mysql-5.7.37-el7-x86_64.tar.gz
mv mysql-5.7.37-el7-x86_64 /usr/local/mysql
3.进行MySQL初始化
在初始化之前,如果有需要,可以创建自己的mysql组和mysql用户;
在初始化的时候指定启动用户;毕竟root用户权限最大,方便运维可以将权限分配出去;
先创建mysql用户并且加入到mysql用户组中:
# 创建mysql用户组
[root@* ~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@* ~]# useradd -g mysql mysql
# 设置密码
[root@* ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
把目录权限改为mysql用户组的mysql用户
[root@* ~]# cd /usr/local/
[root@* mysql]# mkdir /usr/local/mysql/data
[root@* local]# chown -R mysql mysql/
[root@* local]# chgrp -R mysql mysql/
开始初始化。目前mysql_install_db已经淘汰,使用 initialize 指令;
bin/mysqld --initialize --user=mysql --console --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
# (弃用)bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
注意:
改成如上的初始化命令,如果无法获知初始化密码,可以在my.cnf的[mysqld]下配置skip-grant-tables;
my.cnf
[mysqld]
skip-grant-tables
在之后的mysql -uroot -p 命令的时候不需要输入密码直接登录;该方式也适用于忘记root密码的时候;
打印日志如下:
2022-07-18T08:52:04.883228Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-18T08:52:05.206309Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-18T08:52:05.314573Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-18T08:52:05.386152Z 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: e60cd43b-0676-11ed-a0cc-525400d6d8ee.
2022-07-18T08:52:05.390333Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-18T08:52:05.673647Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-18T08:52:05.673663Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-18T08:52:05.674223Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-18T08:52:05.845244Z 1 [Note] A temporary password is generated for root@localhost: 3J=74syIsJWw
这需要注意一点:上面日志中最后一串字符[3J=74syIsJWw]是root密码的临时密码;
在初始化完成之后,记得把root用户密码修改一下;
3.启动MySQL
复制server文件到系统服务目录并授权,方便后面做开机自动启动:
cp ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
执行启动命令
/etc/init.d/mysqld start
设置开机启动:
chkconfig --level 35 mysqld on
查看开机启动列表:
[root@* data]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
4.连接MySQL并且修改root用户密码
先连接数据库,再更改密码
mysql -uroot -p
# 输入上面我们记录的密码:3J=74syIsJWw
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
# 修改密码为123456,这里密码自己设置
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> exit
Bye
进行我们修改之后的密码验证:
[root@* mysql]# mysql -uroot -p
# 输入我们刚才修改的密码
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
5.设置远程访问权限
我们想要使用数据库,还需要设置数据库用户的远程访问权限,不配置远程访问权限,你在使用数据库连接工具(navicat)进行数据库连接的时候会返回:
1130 - Host ‘数据库ip’ is not allowed to connect to this MySQL server
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)
可以看到用户的可以访问的地址host是localhost,也就是说只能本地访问;
修改host配置,这里可以使用update语句进行修改(注意:普通用户是没有权限修改的),这里可以修改指定ip访问,也可以使用通配符;
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> exit
Bye
重启服务之后生效
service mysqld restart