ubuntu mysql 安装

安装与配置MySQL并更改密码
本文详细介绍了在Ubuntu环境下安装MySQL服务器、客户端,并通过命令行界面更改初始密码的过程。包括使用sudo命令进行安装,阅读依赖关系和状态信息,确认额外磁盘空间使用情况,以及通过MySQL客户端更新root用户的密码。

安装

lychie@ubuntu:/setup$ sudo apt-get install mysql-server mysql-client
[sudo] password for lychie: ******
Reading package lists... Done
Building dependency tree
Reading state information... Done
. . . . . .  . . . . . .
After this operation, 93.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
. . . . . .  . . . . . .

 
一路 OK,就行了

更改密码

mysql -uroot -p登录 MySQL,安装的时候没有设置密码,提示输入密码的时候直接回车就完事

lychie@ubuntu:/setup$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)
 
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
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>

update user set password=PASSWORD('******') where user='root';更改密码

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 host, user, password from user;
+-----------+------------------+--------------+
| host      | user             | password     |
+-----------+------------------+--------------+
| localhost | root             |              |
| ubuntu    | root             |              |
| 127.0.0.1 | root             |              |
| ::1       | root             |              |
| localhost | debian-sys-maint | ******       |
+-----------+------------------+--------------+
5 rows in set (0.00 sec)
 
mysql> update user set password=PASSWORD('******') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
 
mysql> select host, user, password from user;
+-----------+------------------+--------------+
| host      | user             | password     |
+-----------+------------------+--------------+
| localhost | root             | ******       |
| ubuntu    | root             | ******       |
| 127.0.0.1 | root             | ******       |
| ::1       | root             | ******       |
| localhost | debian-sys-maint | ******       |
+-----------+------------------+--------------+
5 rows in set (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

启动 / 停止 MySQL

sudo stop mysql     停止 MySQL
sudo start mysql    启动 MySQL

### 如何在 Ubuntu 系统上安装 MySQL #### 更新软件包列表 为了确保获取最新的软件版本,在开始之前应更新系统的软件包索引。这可以通过运行以下命令完成: ```bash sudo apt-get update ``` 此操作会刷新本地的APT缓存并同步最新可用的软件包信息[^1]。 #### 安装 MySQL Server 通过APT包管理器可以直接安装MySQL服务器组件。使用如下命令来实现这一目标: ```bash sudo apt-get install mysql-server ``` 这条指令将会下载并安装MySQL服务端以及必要的依赖项到您的Ubuntu系统中。 另外一种方法涉及手动配置来自官方源的deb文件,这种方法可以更精确控制所安装的具体版本号。具体来说,您需要先加载`mysql-apt-config`这个DEB包,它包含了设置好的APT仓库链接信息。执行下面的shell脚本即可引入这些资源: ```bash dpkg -i mysql-apt-config_0.8.30-1_all.deb ``` 之后再次调用APT工具链重新构建数据库镜像清单,并最终触发实际的产品部署流程[^2]。 请注意无论采用哪种方式都可能被提示输入root账户密码或者确认某些默认选项设定值;按照屏幕上的指示一步步操作下去就可以顺利完成整个过程了! ```python import subprocess def install_mysql(): try: # Update package list and install MySQL server via APT. subprocess.run(['sudo', 'apt-get', 'update'], check=True) result = subprocess.run(['sudo', 'apt-get', 'install', '-y', 'mysql-server'], check=True) if result.returncode != 0: raise Exception("Failed to install MySQL using APT.") except subprocess.CalledProcessError as e: print(f"An error occurred during the installation process: {e}") if __name__ == "__main__": install_mysql() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值