使用apt-get安装的好处是,你不用自己去解决软件之间的依赖问题,基本上apt执行完成,也就把软件安装好了,下面介绍使用apt的方法来安装MySQL,同时也会介绍安装完成后的安全优化。
注意:下面的操作都是以新安装的Ubuntu 15.10来作为演示的。
1.使用apt安装MySQL
-
安装MySQL数据库:
注意:在安装过程中会几次要求你设置MySQL数据库的管理员用户密码,我们这里先不设置。(出现要求设置的页面时直接按“确定”即可。)
1
|
xpleaf@leaf:~$
sudo
apt-get
install
mysql-server mysql-client libmysqlclient-dev
|
注意在你的系统上查看安装成功后的提示信息。
-
开启MySQL服务:
1
|
xpleaf@leaf:~$
sudo
service mysql start
|
-
登陆到MySQL数据库中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
xpleaf@leaf:~$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 7
Server version: 5.6.28-0ubuntu0.15.10.1 (Ubuntu)
Copyright (c) 2000, 2015, 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>
|
由于在安装的时候并没有为MySQL数据库的root用户设置密码,所以这里不用输入密码就可以进入到MySQL数据库中了。
其实如果是非生产环境要求的话,像上面这样使用apt-get简单安装就可以了,当然如果是用于生产环境的,那么建议还是使用源码安装。
2.MySQL安全优化
至于为什么要安全优化,可以看博主写的用源码安装MySQL的博文:《在CentOS上源码安装MySQL+安装问题解决+安全优化》
下面就直接给出操作的步骤。(注意:要确保MySQL服务已经开启,上面已经给出方法)
(1)为root用户创建密码
1
2
3
4
5
6
7
|
xpleaf@leaf:~$ mysql -u root
mysql> update mysql.user
set
password = password(
'123456'
) where User =
'root'
;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
|
上面就为root用户创建了密码`123456`。
执行完之后再查看一下当前的账户信息:
1
2
3
4
5
6
7
8
9
10
11
|
mysql>
select
User, Host, Password from mysql.user;
+------------------+-----------+-------------------------------------------+
| User | Host | Password |
+------------------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | leaf | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| debian-sys-maint | localhost | *6A368D614E978D78292FC3257D3958C6A7B241EF |
+------------------+-----------+-------------------------------------------+
5 rows
in
set
(0.00 sec)
|
需要注意的是,由于我们已经为root用户设置了密码,所以在下次登陆的时候请指定-p参数:
1
2
|
xpleaf@leaf:~$ mysql -u root -p
Enter password:
|
密码就是我们设置的`123456`。
(2)删除test数据库或名字以test开头的数据库
操作如下:
1
2
3
4
5
|
mysql> delete from mysql.db where db like
'test%'
;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
|
OK,到这里的话,使用apt-get安装MySQL以及MySQL的基本安全优化就完成了,应该不会有太大的问题。
本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1748704,如需转载请自行联系原作者