Linux中手动安装MySQL


步骤一:安装MySQL

1.运行以下命令更新YUM源:

查看代码

sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

2.安装MySQL:

查看代码

sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck

3.查看MySQL版本信息:

查看代码

mysql -V
mysql  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)

步骤二:配置MySQL

1.启动MySQL服务

查看代码

systemctl start mysqld

2.设置MySQL服务开机自启动

查看代码

systemctl enable mysqld

3.查看/var/log/mysqld.log文件,获取并修改root用户初始化密码

查看代码

[root@hua30 ~]# grep 'temporary password' /var/log/mysqld.log
2022-03-21T07:55:57.425757Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: w;-14>ww2*E1

4.执行命令:mysql_secure_installation,对MySQL进行安全性配置

查看代码

[root@huan30 ~]# mysql_secure_installation

# 1.重置root用户初始化密码
Enter password for user root: #输入已获取的root用户初始密码

The existing password for the user account root has expired. Please set a new password.

New password: #输入新的MySQL密码

Re-enter new password: #重复输入新的MySQL密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。

New password: #输入新的MySQL密码

Re-enter new password: #重复输入新的MySQL密码

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #输入Y确认使用已设置的密码。

# 2.删除匿名用户
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y #输入Y删除MySQL默认的匿名用户。
Success.

# 3.禁止root账号远程登录
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y #输入Y禁止root远程登录。
Success.
                                
# 4.删除test库以及test库的访问权限
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y #输入Y删除test库以及对test库的访问权限。
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

# 5.重新加载授权表     
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y #输入Y重新加载授权表。
Success.

All done!

5.查看MySQL运行状态:systemctl status mysqld

查看代码

[root@hua30 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2022-03-21 15:56:00 CST; 16h ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 23110 (mysqld)
   Status: "Server is operational"
    Tasks: 40
   Memory: 500.7M
   CGroup: /system.slice/mysqld.service
           └─23310 /usr/sbin/mysqld

3月 21 15:55:54 hua30 systemd[1]: Starting MySQL Server...
3月 21 15:56:00 hua30 systemd[1]: Started MySQL Server.

6.查看MySQL安装路径:find / -name mysql   or    whereis mysql

查看代码

[root@hua30 ~]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
/etc/selinux/targeted/active/modules/100/mysql
查看代码
[root@hua30 ~]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/man/man1/mysql.1.gz

步骤三:登录MySQL

1.登录MySQL数据库:mysql -u root -p,回车输入密码

查看代码

[root@hua30 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 8.0.28 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> 

2.创建MySQL远程登录账号:

注意 实际创建账号时,需将示例密码123456更换为符合要求的密码,并妥善保存。密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:()` ~!@#$%^&*-;‘<>,.?/

查看代码

mysql> create user 'mysql_user'@'%' identified by '123456'; #创建数据库用户mysql_user,并授予远程连接权限。
mysql> grant all privileges on *.* to 'mysql_user'@'%'; #为mysql_user用户授权数据库所有权限。
mysql> flush privileges; #刷新权限。

3.查看创建用户信息及密码解密方式

查看代码

mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | mysql_user       | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password | |
+-----------+------------------+-----------------------+

# plugin为密码解密方式