Ubuntu22.04安装MySQL

1、安装服务器端

1
sudo apt-get install mysql-server

2、安装客户端

1
sudo apt-get install mysql-client

3、安装 mysql 相关库

1
sudo apt-get install libmysqlclient-dev

4、安装成功后查看 MySQL 版本

1
mysql -V

查看MySQL版本

5、修改 mysqld.cnf 配置文件

1
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

(1)在 [mysqld] 最后一行加入:

1
# skip-grant-tables    <-- add # here

(2)然后点击 ESC ,输入 ‘ :wq ’ 进行保存和退出;如下图所示;

注:如果不修改 mysqld.cnf 配置文件,修改完 MySQL 的用户名和密码后,在进入 MySQL 时可能会有如下错误,即语法错误拒绝被访问:

修改配置文件

6、查看默认安装的 MySQL 的用户名和密码

1
sudo cat /etc/mysql/debian.cnf

修改配置文件

7、登录 MySQL ;

1
mysql -u debian-sys-maint -p    # 换行后输入上述查到的密码
查看默认安装的用户名和密码

查看默认安装的用户名和密码

8、进入 MySQL ;

1
use mysql;

9、刷新权限;

1
flush privileges;

10、修改用户名和密码;

1
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123456';

11、刷新权限;

1
flush privileges;

12、退出;

1
quit;

13、重启 MySQL ;

1
service mysql restart

14、进入 MySQL ;

1
mysql -u root -p    # 回车后输入自己修改后的密码即可

若以上过程没有效果,进行以下步骤:

15、修改 mysqld.cnf 配置文件

1
sudo vim /etc/mysql/debian.cnf

修改cnf文件

16、保存退出

键入i 进入,修改user和password
键入Esc,输入:wq 保存退出。退出vi后刷新权限退出MySQL,重启

17、刷新权限;

1
flush privileges;

18、退出;

1
quit;

19、重启 MySQL ;

1
service mysql restart

20、进入 MySQL ;

1
mysql -u root -p    # 回车后输入自己修改后的密码即可

修改成功

MySQL中help手册翻译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
query_attributes Sets string parameters (name1 value1 name2 value2 ...) for the next query to pick up.
ssl_session_data_print Serializes the current SSL session data to stdout or file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
所有MySQL命令的列表:
请注意,所有文本命令都必须位于第一行,并以“;”结尾
? (\?)“帮助”的同义词。
clear(\c)清除当前输入语句。
connect(\r)重新连接到服务器。可选参数为db和host。
delimiter(\d)设置语句分隔符。
edit(\e)使用$EDITOR编辑命令。
ego(\G)向mysql服务器发送命令,垂直显示结果。
exit(\q)退出mysql。与退出相同。
go(\g)将命令发送到mysql服务器。
help(\h)显示此帮助。
nopage(\n)禁用寻呼机,打印到标准输出。
notee(\t)不要写入outfile。
寻呼机(\P)设置pager[到页面]。通过PAGER打印查询结果。
print(\p)打印当前命令。
prompt(\R)更改您的mysql提示符。
quit(\q)退出mysql。
rehash(\#)重新生成完成哈希。
source(\.)执行SQL脚本文件。将文件名作为参数。
status(\s)从服务器获取状态信息。
system(\!)执行系统shell命令。
tee(\T)设置输出文件[to_outfile]。将所有内容附加到给定的外文件中。
use(\u)使用另一个数据库。将数据库名称作为参数。
charset(\C)切换到另一个字符集。可能需要处理具有多字节字符集的binlog。
warnings(\W)在每条语句后显示警告。
nowarning(\w)不要在每条语句后都显示警告。
resetconnection(\x)清除会话上下文。
query_attributes为下一个要提取的查询设置字符串参数(名称1值1名称2值2…)。
ssl_session_data_print将当前ssl会话数据序列化到stdout或文件

其他命令:

更新

1
sudo apt update   

免密码进入mysql:

1
sudo mysql -uroot

参考教程:

Ubuntu 20.04 + mysql 8.0.27 用户名和密码修改

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信