之前双11买了最低配的1核2G的云服务器,到现在还没有使用过,最近想拿这闲置的服务器安装个数据库玩玩,记录下安装过程,以便之后使用。
参考博客:
1、腾讯云安装MySQL
2、查看本地服务器MYSQL的端口号
3、Mysql添加用户,给用户授权
主要内容:
1、在云服务器上安装mysql
2、登录使用mysql
3、Navicat链接数据库mysql
4、数据库用户的增删改查
5、数据库的简单查询语句
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum module disable mysql
会提示 y/n,选择y,再执行下面的命令
yum -y install mysql-community-server
systemctl start mysqld.service
systemctl status mysqld.service
控制台会出现绿色的active(running),详细提示如下:
[root@VM-0-11-centos ~]# systemctl status mysqld.service ● mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2022-01-08 17:12:07 CST; 1 day 15h ago Process: 12194 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS) Process: 12049 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCE> Process: 12024 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS) Main PID: 12145 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 11438) Memory: 483.7M CGroup: /system.slice/mysqld.service └─12145 /usr/libexec/mysqld --basedir=/usr
获取MySQL安装后的临时密码
grep "password" /var/log/mysqld.log
通过显示获取临时密码:
[Note] A temporary password is generated for root@localhost: B*Kop+?0g)lt
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.
临时密码明显是记不住的,所以修改密码是势在必行的。
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)
默认MySQL远程的数据库是仅能localhost访问,使用navicat等工具连接就会提示1130 - Host ‘xxx.xxx.xx.xxx’ is not allow to connect to this MySQL server,如果需要远程访问,需要修改权限
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)
更新User 表 Host 字段为 ‘%’,代表所有远程主机。
update user set Host='%' where User='root';
flush privileges;
show global variables like 'port';
打印信息:
mysql> show global variables like 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.00 sec) mysql>
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
username – 你将创建的用户名称;
host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password – 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
示例:
CREATE USER 'rattan'@'localhost' IDENTIFIED BY '123456'; CREATE USER 'rattan'@'172.20.0.0/255.255.0.0' IDENDIFIED BY '123456'; CREATE USER 'rattan'@'%' IDENTIFIED BY '123456'; CREATE USER 'rattan'@'%' IDENTIFIED BY ''; CREATE USER 'rattan'@'%';
GRANT privileges ON databasename.tablename TO 'username'@'host';
privileges – 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所 的权限则使用ALL说明:
databasename – 数据库名
tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用* 表示, 如*.*
示例:
GRANT SELECT, INSERT ON test.user TO 'rattan'@'%'; GRANT ALL ON *.* TO 'rattan'@'%';
注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
例子说明: privilege, databasename, tablename – 同授权部分
REVOKE SELECT ON *.* FROM 'rattan'@'%';
假如你在给用户’rattan’@’%’授权的时候是这样的(或类似 的):GRANT SELECT ON test.user TO ‘rattan’@’%’, 则在使用 REVOKE SELECT ON . FROM ‘rattan’@’%’;命令并不能撤销该用户对test数据库中user表的SELECT 操作指令
相反,如果授权使用的是GRANT SELECT ON . TO ‘rattan’@’%’;则 REVOKE SELECT ON test.user FROM ‘rattan’@’%’;命令也不能撤销该用户对test数据库中user表的 Select 权限
具体信息可以用命令SHOW GRANTS FOR ‘rattan’@’%’; 查看
DROP USER ‘username’@'host’;
操作后切记刷新数据库
flush privileges;
select database();
输出信息:
+------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec)
select user();
输出信息:
+----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
select version();
输出信息:
+-----------+ | version() | +-----------+ | 8.0.26 | +-----------+ 1 row in set (0.00 sec)
ALTER
Allows use of ALTER TABLE.
ALTER ROUTINE
Alters or drops stored routines.
CREATE
Allows use of CREATE TABLE.
CREATE ROUTINE
Creates stored routines.
CREATE TEMPORARY TABLE
Allows use of CREATE TEMPORARY TABLE.
CREATE USER
Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW
Allows use of CREATE VIEW.
DELETE
Allows use of DELETE.
DROP
Allows use of DROP TABLE.
EXECUTE
Allows the user to run stored routines.
FILE
Allows use of SELECT… INTO OUTFILE and LOAD DATA INFILE.
INDEX
Allows use of CREATE INDEX and DROP INDEX.
INSERT
Allows use of INSERT.
LOCK TABLES
Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
PROCESS
Allows use of SHOW FULL PROCESSLIST.
RELOAD
Allows use of FLUSH.
REPLICATION
Allows the user to ask where slave or master
CLIENT
servers are.
REPLICATION SLAVE
Needed for replication slaves.
SELECT
Allows use of SELECT.
SHOW DATABASES
Allows use of SHOW DATABASES.
SHOW VIEW
Allows use of SHOW CREATE VIEW.
SHUTDOWN
Allows use of mysqladmin shutdown.
SUPER
Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
UPDATE
Allows use of UPDATE.
USAGE
Allows connection without any specific privileges.