ubuntu 安装mysql server

ubuntu 安装mysql server

ubuntu 安装配置 mysql 记录备忘。

环境

  • 系统:ubuntu 18.04 LTS
  • 安装方式:apt
  • 版本:5.7.25

操作

安装

1
sudo apt install mysql

启用服务

1
2
3
sudo systemctl start mysql  //启用服务
sudo systemctl status mysql //查看服务状态
sudo systemctl enable mysql //将服务添加开机启动

初始化操作

在之前的ubuntu版本中,通过apt方式安装mysql服务后,直接运行命令mysql_secure_installation,即可直接运行mysql的初始化。默认的root密码为空,提交回车后,会直接提示设置root密码,以及mysql相关的其它配置。

在ubuntu 18.04中,默认的安装中,没有root账号,运行会提示如下:

Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user ‘root‘@’localhost’

即没有root账号也没有无法运行mysql_secure_installation命令,需要我们从安装的配置文件中寻找有用的信息,运行命令:

1
whereis mysql

输出结果:

mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

找到配置文件目录:

1
ls /etc/mysqlconf.d  debian.cnf  debian-start  my.cnf  my.cnf.fallback  mysql.cnf  mysql.conf.d

在配置目录中,还看到了debian.cnf,查看配置文件:

1
cat /etc/mysql/debian.cnf

输出如下:

Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = ABCDEFGHIJKLMNOPQ
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = ABCDEFGHIJKLMNOPQ
socket = /var/run/mysqld/mysqld.sock

好了,从配置文件中,我们看到了初始生成的用户名和密码了,使用用户名和密码登录,成功进入。

创建用户及授权

使用默认生成的账号和密码登录:

1
mysql -u debain-sys-maint -p
1
CREATE USER 'your_name'@'localhost' IDENTIFIED BY 'password';

授权:

1
GRANT ALL PRIVILEGES ON *.* TO 'your_name'@'localhost' WITH GRANT OPTION

授权所有权限,并授权从任意位置登录。

以上操作至此结束。

查看已存在的用户密码:

1
select user,host from mysql.user;

更改用户密码:

例如更新那个不知道密码的root账号:

1
2
3
4
5
6
mysql> update user set authentication_string=password('new_password') WHERE user='root';
Query OK, 1 row affected, 1 warning (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges; //刷新权限,立即生效
Query OK, 0 rows affected (0.00 sec)

ubuntu 安装mysql server
https://ywmy.xyz/2019/03/06/ubuntu-安装mysql-server/
作者
ian
发布于
2019年3月6日
许可协议