MySQL的基本操作

1.启动与关闭

systemctl start/stop mysqld.service

2.登录与退出

开始登录

mysql -u用户 -p密码 -hIP地址 -D数据库名 -P端口

注意:-p密码 不能有空格,密码有特殊字符,需要用单引号括起来。8.0禁止将密码写在命令行

[root@localhost ~]# mysql -uroot -p

不知晓密码的情况下我们可以使用临时密码

[root@localhost ~]# grep password /var/log/mysqld.log   

 

进入mysql

mysql> select version();

mysql> select database();

注意:

1.注意命令最后有';'

2.例如若出现这种情况,一般是命令输入不完整,如:

命令输入完成后会出现两种情况:

1.此时若使用临时密码将出现以下提示。如果提示使用临时密码请修改密码(使用初始密码将给出以下提示,我们可以忽略提示或者 修改root的密码:密码要复合复杂性要求。)

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

2.若密码已经修改,我们会直接进入为这个界面。

查看当前选择数据库

mysql> show database;

3.修改root密码

方法1:mysqladmin

[root@localhost ~]# mysqladmin -uroot -p password '#OPENlab123'
[root@localhost ~]# mysqladmin -uroot -p password 

最好不要把密码显示在命令行。系统会进行提示警告危险。所以我们最好使用第二个命令同样达到修改密码的目的。

方法2:alter user

mysql> alter user root@localhost identified by '#OPENlab123';
Query OK, 0 rows affected (0.00 sec)

方法3:set password for

mysql> set password for 'root'@'localhost' = '#openLAB123';
Query OK, 0 rows affected (0.01 sec)

方法4:update 表

mysql> update user set authentication_string='' where user='root';
mysql> flush privileges;刷新权限表
​
mysql> update user set authentication_string='' where user='root';
ERROR 1046 (3D000): No database selected(若出现此问题,解决方式在其后)
​
mysql> update mysql.user set authentication_string='' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

然后刷新权限表,此时该数据库可以不使用密码登录。(此方法不安全)

出现的问题:

mysql> update user set authentication_string='' where user='root';
ERROR 1046 (3D000): No database selected

解决方法:

mysql> select database();     ###   查看当前选择的数据库
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)
​
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客户端是支持命令命令历史的

4.root重置密码

方法1

停止服务:

[root@mysql ~]# systemctl stop mysqld

启动时跳过权限表:

[root@mysql ~]# mysqld --user=mysql --skip-grant-tables

输入该命令后,页面将一直保持静止的状态,无法进行操作。我们双击窗口栏新添加一个窗口进行后续操作。相当于我们已经跳过权限直接进入mysql。

刷新权限表:

mysql> flush privileges;

修改密码:

可以使用上述方法2

mysql> alter user root@localhost identified by '#openLAB123';

也可以上述方法4

mysql> update user set authentication_string='' where user='root';

此时密码已经修改完成

恢复进程:

我们先查看进程进度

[root@localhost ~]# ps -ef | grep mysql

阻止进程:

[root@localhost ~]# kill -9 3227

此时我们查看前端口发现已经恢复

方法2

创建一个文件并输入内容

[root@mysql ~]# vim /tmp/mysql-init 

在文件中输入

alter user 'root'@'localhost' identified by '#OPENlab123';

查找日志

[root@mysql ~]# tail -f /var/log/mysqld.log

mysql处于一个等待连接的状态。我们切换到另一个窗口

查看mysql状态

[root@localhost ~]# ps -ef | grep mysql

关闭mysql

[root@localhost ~]# systemctl stop mysqld

启动

[root@mysql ~]# mysqld --init-file=/tmp/mysql-init --user=mysql &

查看mysql状态

[root@localhost ~]# ps -ef | grep mysql

 

停止数据库

[root@localhost ~]# kill -9 4558

 

5.用户名&主机名

User Value          Host Value                  Permissible Connections

'fred'                  'h1.example.net'          fred, connecting from h1.example.net

'  '                      'h1.example.net'          Any user, connecting from h1.example.net

'fred'                     '%'                            fred, connecting from any host

'  '                         '%'                            Any user, connecting from any host

'fred'                  '%.example.net'          fred, connecting from any host in the example.net domain

'fred'                  'x.example.%'             fred, connecting from x.example.net, x.example.com, x.example.edu, and so on; this is probably not useful

'fred'                  '198.51.100.177'        fred, connecting from the host with IP address 198.51.100.177 '

fred'                  '198.51.100.%'           fred, connecting from any host in the 198.51.100 class C subnet 'fred' '198.51.100.0/255.255.255.0' Same as previous example

6.查询命令

6.1 查询当前用户

mysql> select current_user();

6.2 查询数据库

mysql> select database();

6.3 切换数据库并查询数据库

mysql> use mysql;
mysql> select database();

7.配置root远程登陆

配置root密码

alter user root@localhost  identified by '#OPENlab123';

更新为所有主机

update user set host='%' where user='root';

遇到问题,命令不成功:

解决办法:重新选择mysql

刷新权限表

mysql> flush privileges;

查看防火墙

[root@localhost ~]# systemctl status firewall

添加防火墙

[root@localhost ~]# firewall-cmd --permanent --add-service=mysql
[root@localhost ~]# firewall-cmd --reload

8、Mysql客户端工具的使用

图形化管理工具

    https://www.navicat.com.cn/            1
    
    https://dev.mysql.com/downloads/workbench/          2
    
    https://dbeaver.io/download/                    3
    
    https://webyog.com/product/sqlyog/

连接时一般需要:

1、 会话名称 2、 主机地址 ip/主机名 3、 端口 4、 数据库服务器的用户名 5、 数据库服务器的密码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是MySQL基本操作的步骤: 1. 安装MySQL:首先需要在计算机上安装MySQL数据库。可以从官网下载MySQL安装程序,并根据安装向导进行安装。 2. 启动MySQL服务:安装完成后,启动MySQL服务。如果是在Windows系统上安装的MySQL,可以在服务中查找到MySQL服务,并手动启动。 3. 登录MySQL:启动MySQL服务后,可以使用命令行工具或者MySQL客户端登录MySQL数据库。如果使用命令行工具,可以使用以下命令登录: ``` mysql -u用户名 -p密码 ``` 如果使用MySQL客户端,可以在界面中输入用户名和密码登录MySQL。 4. 创建数据库:登录MySQL后,可以使用以下命令创建数据库: ``` create database 数据库名; ``` 5. 创建表:在创建好数据库后,可以使用以下命令在数据库中创建表: ``` create table 表名( 字段1 数据类型, 字段2 数据类型, ... ); ``` 6. 插入数据:创建好表后,可以使用以下命令向表中插入数据: ``` insert into 表名(字段1, 字段2, ...) values (值1, 值2, ...); ``` 7. 查询数据:使用以下命令可以查询表中的数据: ``` select * from 表名; ``` 可以根据需要添加查询条件和排序规则,例如: ``` select * from 表名 where 条件 order by 排序规则; ``` 8. 更新数据:使用以下命令可以更新表中的数据: ``` update 表名 set 字段1=值1, 字段2=值2 where 条件; ``` 9. 删除数据:使用以下命令可以删除表中的数据: ``` delete from 表名 where 条件; ``` 10. 关闭MySQL:使用以下命令可以关闭MySQL服务: ``` quit; ``` 以上就是MySQL基本操作的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值