1、序言
去年双11买了最低配的1核2G的数据库,之前用来搭建自己的博客,之后用GitHub搭建hexo了,闲置的服务器就想安装个数据库啥的玩玩,记录下安装过程,以便之后使用。
参考博客:腾讯云安装MySQL
2、下载安装指令
2.1 下载安装包
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2.2 安装rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
2.3 安装MySQL服务
百度上说执行下面的命令就行,但是我在安装的时候出错 Error: Unable to find a match: mysql-community-server
yum -y install mysql-community-server # 无效命令
百度之后执行下面的两条命令有效:
yum module disable mysql
会提示 y/n,选择y,再执行下面的命令
yum -y install mysql-community-server
2.4 启动MySQL
systemctl start mysqld.service
2.5 查看MySQL运行状态
systemctl status mysqld.service
控制台会出现绿色的active(running),详细提示如下:
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-05-09 00:06:09 CST; 44s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 7390 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 7235 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 7393 (mysqld)
Tasks: 27 (limit: 11507)
Memory: 360.6M
CGroup: /system.slice/mysqld.service
└─7393 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
2.6 MySQL 安装后的临时密码
获取MySQL安装后的临时密码
grep "password" /var/log/mysqld.log
通过显示获取临时密码:
[Note] A temporary password is generated for root@localhost: B*Kop+?0g)lt
3、登录使用MySQL
3.1 控制台远程登陆MySQL
mysql -u root -p
输入上面的临时密码,得到以下提示就表示登陆成功:
[root@VM-0-16-centos ~]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.34Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
3.2 修改密码
临时密码明显是记不住的,所以修改密码是势在必行的。
set password for 'root'@'localhost'=password('xxxxxx');
上面指令的root是用户名称,xxxxxx是密码,写一个自己记得住的。密码必须要包含大小写字符和数字,特殊字符也可以用,太简单会提示错误:Your password does not satisfy the current policy
提示信息:
set password for 'root'@'localhost'=password('xxxxxx');Query OK, 0 rows affected, 1 warning (0.00 sec)
3.3 查询数据库访问权限
默认MySQL远程的数据库是仅能localhost
访问,使用navicat等工具连接就会提示1130 - Host 'xxx.xxx.xx.xxx' is not allow to connect to this MySQL server
,如果需要远程访问,需要修改权限
注意MySQL指令结尾需要写分号
;
,不然不能执行
use mysql;
提示信息:
Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed
继续执行命令:
select * from user;
提示信息:
+-----------+---------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked |+-----------+---------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+| localhost | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *679B7F329AC7191F6B5B11E97E3458147C8B4339 | N | 2021-05-09 00:25:27 | NULL | N || localhost | mysql.session | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N | 2021-05-09 00:06:05 | NULL | Y || localhost | mysql.sys | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N | 2021-05-09 00:06:05 | NULL | Y |+-----------+---------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+3 rows in set (0.00 sec)
3.4 修改数据库访问权限
更新User 表 Host 字段为 ‘%’,代表所有远程主机。
update user set Host='%' where User='root';
3.5 刷新数据库访问权限
flush privileges;
3.6 连接数据库
经过上述操作,使用Navicat工具等连接数据库,反正我的已经连接成功了。
南风知我意,吹梦到西洲。