本文共 3102 字,大约阅读时间需要 10 分钟。
假设用户名和密码均为 root
:
mysql -u root -p
回车后输入密码 root
回车即可进入。
或直接使用:
mysql -uroot -proot
注意:用户名前空格可有可无,密码前必须没有空格。
假设远程主机IP为 110.110.110.110
,用户名和密码均为 root
:
mysql -h 110.110.110.110 -uroot -proot
exit
alter user root@localhost identified by 'newroot'
或主机地址为空时:
alter user root identified by 'newroot'
mysql -uroot -prootselect user, host from user;
host
列即为主机地址。
create user 'tom1'@'localhost' identified by 'tom1';
或省略引号:
create user tom1@localhost identified by 'tom1';
或指定主机地址:
create user tom1@'192.168.1.%' identified by 'tom1';
mysql> create database testdb;
mysql> show databases;
mysql> drop database testdb;
或删除不存在的数据库:
mysql> drop database if exists testdb;
mysql> use testdb;
mysql> select database();
create table class ( id int(4) not null primary key auto_increment comment '主键', name varchar(20) not null comment '姓名', sex int(4) not null default '0' comment '性别', degree double(16,2) default null comment '分数');
mysql> show full fields from class;
mysql> drop table mytable;
或删除不存在的表:
mysql> drop table if exists mytable;
mysql> insert into class (name, sex, degree) values('charles', '1', '80.5');
mysql> insert into class (name, sex, degree) values('charles', '1', '80.5'), ('tom', '1', '80.5');
mysql> select * from class;
mysql> select * from class limit 2;
mysql> delete from class where id = 9;
mysql> update class set degree = '90.9' where id = 10;
mysql> alter table class add exam_type int(4) default null comment '考试类别' after sex;
mysql> alter table class change name name_new varchar(50) not null;
mysql> alter table class drop column remark;
mysql> alter table class add constraint pk_id primary key using btree (id);
mysql> alter table class add unique key uk_name using btree (name);
mysql> alter table class drop key uk_name;
mysql> alter table class add index name_index (name);
mysql> alter table class drop index name_index;
mysql> alter user root@localhost identified by 'newpassword';
mysql> create user 'newuser'@'localhost' identified by 'newpassword';
mysql> drop user 'newuser'@'localhost';
mysql> select * from mysql.user;
describe class;
explain statement "SELECT * FROM class LIMIT 10";
show global variables like 'max_connections';
show table status \G;
show triggers;
show create table class\G;
select now();
select date_format(now(), '%Y-%m-%d %H:%i:%s');
select from_unixtime(unix_timestamp('2024-01-01 12:00:00'));
mysqldump -u root -p --databases test > test.backup.sql
mysql -u root -p < test.backup.sql
通过以上命令,您可以完成MySQL的基本操作。如果需要更详细的操作说明或高级功能,请参考MySQL官方文档或相关技术资料。
转载地址:http://rjbfk.baihongyu.com/