Ubuntu下MySql安装

203 阅读3分钟

本文正在参加「技术专题19期 漫谈数据库技术」活动

MySQL的选择

数据库(DataBase) 是按照数据结构来组织、存储和管理数据的仓库。MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,属于Oracle旗下产品。MySQL是开源免费的,加上操作简便,轻量易部署,因此在全球范围得到了广泛的传播和使用,成为个人开发者最为喜爱的数据库产品。

相信每一个开发者无论对数据库了解多少,如果需要使用到数据库,首选就是MySQL。比如学生时代常做的学生管理系统,成绩管理系统,酒店管理系统等等,均使用MySQL来存储管理列表,增删改查比使用文件操作要更为便捷高效。并且使用数据库可扩展性强,容易将简单系统迭代为一个大型系统,而不用在数据管理上做出太大修改。

MySql安装

在Ubuntu上安装MySQL数据库,一般分为两种方法,分别是使用Ubuntu Software Center或者apt命令来安装,过程都比较简单,但我们在Linux下习惯于使用命令行进行安装。此次安装我们的系统是ubuntu 18.04。

更新系统

更新系统,保证系统处于当前版本的最新状态:

sudo apt-get update

安装Mysql

直接通过系统命令安装:

sudo apt-get install mysql-server

image.png

Mysql初始化

安装完后才能后进行MySQL的初始化配置,详细选择建下图:

sudo mysql_secure_installation

配置如下:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 25 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 

查看运行状态

sudo service mysql status

image.png

登录数据库

登录mysql数据库可以通过如下命令:

sudo mysql -uroot -p

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,在Enter password:处输入前面设置的密码回车,就能够进入mysql数据库。 进入数据后输入show databases;即可查看数据库:

image.png

输入use mysql即可使用数据库,

查询表单数据,输入select user,host,plugin from user;查询出来的是mysql数据库的所有账户信息:

image.png

MySQL服务启停

systemctl start mysql.service
systemctl stop mysql.service

本文正在参加「技术专题19期 漫谈数据库技术」活动