怎么更改MySQL root用户口令
发布时间:2021-12-16 09:39:59 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要讲解了怎么修改MySQL root用户口令,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习怎么修改MySQL root用户口令吧! 1.一些基本概念 1)MySQL体系结构由五个主子系统组成:查询引擎、存储管理器
这篇文章主要讲解了“怎么修改MySQL root用户口令”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么修改MySQL root用户口令”吧! 1.一些基本概念 1)MySQL体系结构由五个主子系统组成:查询引擎、存储管理器、缓冲管理器、事务管理器和恢复管理器。 2)查询引擎包含三个相关联的部件:语法分析器、查询优化器和执行部件。 3)除五个主子系统以外,MySQL体系结构还包括两个辅助部件:过程管理器和函数库。 4)在MySQL中,事务的开始标记为一个BEGIN语句(与Oracle不同)。 2.安装完成后修改MySQL root用户口令 C:Documents and SettingsAdministrator>mysql -u root Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 18 Server version: 5.1.34-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> use mysql Database changed mysql> set password for 'root'@'localhost' = password('passwd'); Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [@more@]C:Documents and SettingsAdministrator>mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: N O) C:Documents and SettingsAdministrator>mysql -u root -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 20 Server version: 5.1.34-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. 3.修改数据文件存放路径 先关闭MySQL服务器: C:Documents and SettingsAdministrator>mysqladmin -u root -p shutdown Enter password: ****** 修改my.ini配置文件(默认放在 D:Program FilesMySQLMySQL Server 5.1 下)中的datadir参数: #Path to the database root datadir="D:MySQL Datafilesdata" 启动MySQL服务器,可以通过启动系统服务的方法。 4.一系列简单操作 1)创建数据库,查看数据库,选择想要使用的数据库 mysql> create database ggyy; Query OK, 1 row affected (0.43 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ggyy | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ggyy | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use ggyy Database changed 2)创建表,查看表 mysql> create table members -> ( -> id int(3) auto_increment, -> fname varchar(20) not null, -> lname varchar(20) not null, -> tel varchar(15), -> email varchar(50), -> primary key (id) -> ); Query OK, 0 rows affected (0.49 sec) mysql> show tables; +----------------+ | Tables_in_ggyy | +----------------+ | members | +----------------+ 1 row in set (0.01 sec) 注:auto_increment修饰符只适用于整型字段,表明MySQL将要为这个字段自动生成一个数字(通过对前面的值增加1)。一个表只能有一个auto_increment字段,而且这个字段必须被定义为键(即字段上必须有索引,术语“键”和“索引”在MySQL中是等同的)。 这时可以在数据文件的存放路径下看到新生成的文件: D:MySQL Datafilesdataggyy>dir Volume in drive D is Data Volume Serial Number is D632-9209 Directory of D:MySQL Datafilesdataggyy 2009-05-18 10:58 . 2009-05-18 10:58 .. 2009-05-18 10:18 65 db.opt 2009-05-18 10:36 8,680 members.frm 2 File(s) 8,745 bytes 2 Dir(s) 66,038,996,992 bytes free 3)添加列,修改列,删除列 mysql> desc members; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | fname | varchar(20) | NO | | NULL | | | lname | varchar(20) | NO | | NULL | | | tel | varchar(15) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec) mysql> alter table members add remark varchar(50); Query OK, 0 rows affected (0.67 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc members; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | fname | varchar(20) | NO | | NULL | | | lname | varchar(20) | NO | | NULL | | | tel | varchar(15) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | remark | varchar(50) | YES | | NULL | | +--------+-------------+------+-----+---------+----------------+ 6 rows in set (0.04 sec) mysql> alter table members modify remark varchar(100); Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc members; +--------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | fname | varchar(20) | NO | | NULL | | | lname | varchar(20) | NO | | NULL | | | tel | varchar(15) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | remark | varchar(100) | YES | | NULL | | +--------+--------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec) mysql> alter table members drop remark; Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc members; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | fname | varchar(20) | NO | | NULL | | | lname | varchar(20) | NO | | NULL | | | tel | varchar(15) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec) 4)插入记录,查询表,修改记录,删除记录 mysql> insert into members (id, fname, lname, tel, email) values (1, 'Yue', 'Gao ', '1234567', 'yuegao@company.com'); Query OK, 1 row affected (0.07 sec) mysql> insert into members values (3, 'Feng', 'Song', '7654321', 'fengsong@compa ny.com'); Query OK, 1 row affected (0.43 sec) mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 3 | Feng | Song | 7654321 | fengsong@company.com | +----+-------+-------+---------+----------------------+ 2 rows in set (0.00 sec) mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 3 | Feng | Song | 7654321 | fengsong@company.com | +----+-------+-------+---------+----------------------+ 2 rows in set (0.01 sec) mysql> insert into members (fname, lname, email) values ('Chen', 'Chu', 'chenchu @company.com'); Query OK, 1 row affected (0.44 sec) mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 3 | Feng | Song | 7654321 | fengsong@company.com | | 4 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+---------+----------------------+ 3 rows in set (0.00 sec) 这里可以看到auto_increment修饰符的作用,自动将新插入的记录id段的值设置为4,而不是2。 mysql> update members set id = 2 where id = 3; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | | 4 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+---------+----------------------+ 3 rows in set (0.00 sec) mysql> delete from members where id = 4; Query OK, 1 row affected (0.43 sec) mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | +----+-------+-------+---------+----------------------+ 2 rows in set (0.00 sec) mysql> insert into members (fname, lname, email) values ('Chen', 'Chu', 'chenchu @company.com'); Query OK, 1 row affected (0.43 sec) mysql> select * from members; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | | 5 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+---------+----------------------+ 3 rows in set (0.00 sec) 注:这里可以看到新插入的记录id字段的值为5,即使表中已不存在id为3或4的记录。也就是说,auto_increment修饰符不会重用那些曾经使用过的值。 5)limit关键字 使用limit关键字可以对结果集进行限制,它有两个参数,用逗号隔开,分别指定从哪行开始显示和显示多少行。如果只跟一个参数,则指定从结果集合的开头返回的行数。 mysql> select * from members limit 3,3; Empty set (0.00 sec) mysql> select * from members limit 2,3; +----+-------+-------+------+---------------------+ | id | fname | lname | tel | email | +----+-------+-------+------+---------------------+ | 5 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+------+---------------------+ 1 row in set (0.01 sec) mysql> select * from members limit 1, 3; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 2 | Feng | Song | 7654321 | fengsong@company.com | | 5 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+---------+----------------------+ 2 rows in set (0.00 sec) mysql> select * from members limit 0, 3; +----+-------+-------+---------+----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | | 5 | Chen | Chu | NULL | chenchu@company.com | +----+-------+-------+---------+----------------------+ 3 rows in set (0.00 sec) 可以看到行号是从0开始的,并且3条记录的行号是连续的。id为2的记录是删除后重新插入的,重新插入时id为5的记录已经存在,但id为2的记录还是排在了前面。 mysql> insert into members values (6, 'Chao', 'Zhang', '6666666', 'chaozhang@com pany.com'); mysql> select * from members limit 3,4; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 6 | Chao | Zhang | 6666666 | chaozhang@company.com | +----+-------+-------+---------+-----------------------+ 1 row in set (0.00 sec) mysql> select * from members limit 2,4; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 5 | Chen | Chu | NULL | chenchu@company.com | | 6 | Chao | Zhang | 6666666 | chaozhang@company.com | +----+-------+-------+---------+-----------------------+ 2 rows in set (0.00 sec) 插入一条id为6的记录,它排在了id为5的记录之后。再插入两条记录进行观察: mysql> insert into members values (8, 'Chen', 'Zhang', '8888888', 'chenzhang@com pany.com'); Query OK, 1 row affected (0.43 sec) mysql> insert into members values (7, 'Yifei', 'Yin', '7777777', 'yfyin@company. com'); Query OK, 1 row affected (0.42 sec) mysql> select * from members limit 5,6; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 8 | Chen | Zhang | 8888888 | chenzhang@company.com | +----+-------+-------+---------+-----------------------+ 1 row in set (0.00 sec) mysql> select * from members limit 4,6; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 7 | Yifei | Yin | 7777777 | yfyin@company.com | | 8 | Chen | Zhang | 8888888 | chenzhang@company.com | +----+-------+-------+---------+-----------------------+ 2 rows in set (0.01 sec) mysql> select * from members limit 3,6; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 6 | Chao | Zhang | 6666666 | chaozhang@company.com | | 7 | Yifei | Yin | 7777777 | yfyin@company.com | | 8 | Chen | Zhang | 8888888 | chenzhang@company.com | +----+-------+-------+---------+-----------------------+ 3 rows in set (0.00 sec) 可以看到,不管是重新插入删除掉的记录的也好,让id值较大的记录比id值较小的记录先插入也好,行号的大小顺序好像都是由id字段的大小顺序决定的。而“select * from members;”的结果则体现了行号的大小顺序。 mysql> select * from members; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | | 5 | Chen | Chu | NULL | chenchu@company.com | | 6 | Chao | Zhang | 6666666 | chaozhang@company.com | | 7 | Yifei | Yin | 7777777 | yfyin@company.com | | 8 | Chen | Zhang | 8888888 | chenzhang@company.com | +----+-------+-------+---------+-----------------------+ 6 rows in set (0.00 sec) 为了确定这种猜测,再做一组实验: mysql> create table members_temp -> ( -> id int(3), -> fname varchar(20), -> lname varchar(20), -> tel varchar(15), -> email varchar(50) -> ); Query OK, 0 rows affected (0.48 sec) mysql> insert into members_temp (id, fname, lname) values (1, 'Yue', 'Gao'); Query OK, 1 row affected (0.43 sec) mysql> insert into members_temp (id, fname, lname) values (2, 'Feng', 'Song'); Query OK, 1 row affected (0.03 sec) mysql> insert into members_temp (id, fname, lname) values (5, 'Chen', 'Chu'); Query OK, 1 row affected (0.43 sec) mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +------+-------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from members_temp limit 2,3; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 5 | Chen | Chu | NULL | NULL | +------+-------+-------+------+-------+ 1 row in set (0.01 sec) mysql> select * from members_temp limit 1,3; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 2 | Feng | Song | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +------+-------+-------+------+-------+ 2 rows in set (0.00 sec) mysql> select * from members_temp limit 0,3; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +------+-------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> insert into members_temp (id, fname, lname) values (3, 'Yifei', 'Yin'); Query OK, 1 row affected (0.42 sec) mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | +------+-------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> select * from members_temp limit 3,4; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 3 | Yifei | Yin | NULL | NULL | +------+-------+-------+------+-------+ 1 row in set (0.00 sec) mysql> select * from members_temp limit 2,4; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 5 | Chen | Chu | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | +------+-------+-------+------+-------+ 2 rows in set (0.00 sec) mysql> delete from members_temp where id = 2; Query OK, 1 row affected (0.42 sec) mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | +------+-------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> insert into members_temp (id, fname, lname) values (2, 'Feng', 'Song'); Query OK, 1 row affected (0.03 sec) mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 2 | Feng | Song | NULL | NULL | +------+-------+-------+------+-------+ 4 rows in set (0.00 sec) 到这里可以看到,新创建的表没有索引,也没有主键,不管是让id值较大的记录比id值较小的记录先插入也好,重新插入删除掉的记录的也好,行号都是由记录插入的先后顺序决定的。 mysql> create index id_idx on members_temp (id); Query OK, 4 rows affected (0.71 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 2 | Feng | Song | NULL | NULL | +------+-------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> delete from members_temp where id = 5; Query OK, 1 row affected (0.03 sec) mysql> insert into members_temp (id, fname, lname) values (5, 'Chen', 'Chu'); Query OK, 1 row affected (0.04 sec) mysql> select * from members_temp; +------+-------+-------+------+-------+ | id | fname | lname | tel | email | +------+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +------+-------+-------+------+-------+ 4 rows in set (0.00 sec) 在id列上创建索引,依然如此。 mysql> alter table members_temp add constraint primary key (id); Query OK, 4 rows affected (0.64 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from members_temp; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 4 rows in set (0.01 sec) mysql> delete from members_temp where id = 2; Query OK, 1 row affected (0.45 sec) mysql> insert into members_temp (id, fname, lname) values (2, 'Feng', 'Song'); Query OK, 1 row affected (0.03 sec) mysql> select * from members_temp; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> select * from members_temp limit 3,4; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 1 row in set (0.00 sec) mysql> select * from members_temp limit 2,4; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 3 | Yifei | Yin | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 2 rows in set (0.00 sec) mysql> select * from members_temp limit 1,4; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 2 | Feng | Song | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from members_temp limit 0,4; +----+-------+-------+------+-------+ | id | fname | lname | tel | email | +----+-------+-------+------+-------+ | 1 | Yue | Gao | NULL | NULL | | 2 | Feng | Song | NULL | NULL | | 3 | Yifei | Yin | NULL | NULL | | 5 | Chen | Chu | NULL | NULL | +----+-------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> select * from members; +----+-------+-------+---------+-----------------------+ | id | fname | lname | tel | email | +----+-------+-------+---------+-----------------------+ | 1 | Yue | Gao | 1234567 | yuegao@company.com | | 2 | Feng | Song | 7654321 | fengsong@company.com | | 5 | Chen | Chu | NULL | chenchu@company.com | | 6 | Chao | Zhang | 6666666 | chaozhang@company.com | | 7 | Yifei | Yin | 7777777 | yfyin@company.com | | 8 | Chen | Zhang | 8888888 | chenzhang@company.com | +----+-------+-------+---------+-----------------------+ 6 rows in set (0.00 sec) 在id列上添加主键后,行号变为由id列的大小顺序决定,这就证明了之前的猜想。 感谢各位的阅读,以上就是“怎么修改MySQL root用户口令”的内容了,经过本文的学习后,相信大家对怎么修改MySQL root用户口令这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注! (编辑:临夏站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐