重启mysql进程
root@hecs-91449:/# ./etc/init.d/mysql restart
Restarting mysql (via systemctl): mysql.service.
mysql临时密码登录
root@hecs-356455:~# sudo cat /etc/mysql/debian.cnf #查看临时密码
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint #临时用户
password = MP0MoX9ivLNjkhdG #临时密码
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = MP0MoX9ivLNjkhdG
socket = /var/run/mysqld/mysqld.sock
root@hecs-356455:~# mysql -u debian-sys-maint -p #登录
Enter password: MP0MoX9ivLNjkhdG
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 320
Server version: 8.0.31-0ubuntu0.20.04.2 (Ubuntu)
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>
mysql修改root密码
用户名:root
修改后密码:password
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 user,host from user where user='root';
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)
mysql>
mysql> alter user 'root'@'localhost' identified with caching_sha2_password by 'password';
Query OK, 0 rows affected (0.01 sec)
登录mysql控制台(windows)
首先设置mysql的环境变量,其次输入以下命令
C:\WINDOWS\system32>mysql -uroot -pcmcc1234
-u后面是用户名,默认安装的超级用户是root
-p后面是密码,默认安装有一个随机密码,可以用命令修改
登录mysql控制台(linux)
root@hecs-356455:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 317
Server version: 8.0.31-0ubuntu0.20.04.2 (Ubuntu)
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>
查看mysql的用户
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 user,host from user where user='root';
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)
mysql>
退出mysql控制台
mysql> exit
查看mysql中所有数据库(show databases;)
mysql> show databases;
databases是根数据库
新增数据库(create database test;)
mysql> create database test;
test是新增的数据库名称
切换工作于某数据库(use test;)
mysql> use test;
test是数据库名称
删除某个数据库(drop database test;)
mysql> drop database test;
test是数据库名称
查看某数据库下的所有表(show tables;)
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> show tables;
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| replication_group_configuration_version |
| replication_group_member_actions |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+------------------------------------------------------+
37 rows in set (0.00 sec)
mysql>
查询某张表下所有数据(select * from student;)
mysql> show databases; #查看所有数据库
+--------------------+
| Database |
+--------------------+
| django_mysql |
| information_schema |
| mysql |
| performance_schema |
| phpmyadmin |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> use django_mysql #切换工作的数据库
Database changed
mysql> show tables; #查看当前数据库下所有表
+----------------------------+
| Tables_in_django_mysql |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| student |
+----------------------------+
11 rows in set (0.00 sec)
mysql> select * from student; #查询某张表下所有数据
+------------+
| studentNum |
+------------+
| 111 |
+------------+
1 row in set (0.00 sec)
mysql>
清空某数据库下的某张表的全部数据(DELETE FROM test;)
首先切换工作于某数据库use,其次输入以下命令。
mysql> DELETE FROM test;
Query OK, 3 rows affected (0.12 sec)
mysql> SELECT * FROM test;
Empty set (0.00 sec)
在某数据库中执行sql脚本
#在某数据库中执行sql脚本:注意是在cmd下执行以下命令,不是在mysql下!!
#在linux系统也同理,不要在mysql下执行以下命令。
[mysql的bin目录]\mysql -u用户名 -p密码 -D数据库<[sql脚本文件所在目录]\sql脚本文件全名