-
下载MySQL 5.6
-
安装准备:安装MySQL依赖函数库
-
安装与运行MySQL
-
优化MySQL
(1)账户安全优化
(2)数据库安全优化
1
2
|
[root@leaf ~]
# ls
mysql-5.6.29-linux-glibc2.5-x86_64.
tar
.gz
|
1
2
3
4
5
6
|
[root@leaf ~]
# yum search libaio #查找libaio的相关信息
[root@leaf ~]
# yum install libaio #安装libaio
Loaded plugins: security
Setting up Install Process
Package libaio-0.3.107-10.el6.x86_64 already installed and latest version
Nothing to
do
|
1
2
|
[root@leaf ~]
# groupadd mysql
[root@leaf ~]
# useradd -r -g mysql -s /bin/false mysql
|
1
2
3
|
[root@leaf ~]
# cd /usr/local
[root@leaf
local
]
# tar zxvf /root/mysql-5.6.29-linux-glibc2.5-x86_64.tar.gz
[root@leaf
local
]
# ln -s /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/ mysql
|
1
2
3
|
[root@leaf
local
]
# cd mysql
[root@leaf mysql]
# ls -d data/
data/
|
1
2
3
4
5
|
[root@leaf mysql]
# chown -R mysql . #修改mysql目录下的所有文件的属主为mysql
[root@leaf mysql]
# chgrp -R mysql . #修改mysql目录下的所有文件的属组为mysql
[root@leaf mysql]
# scripts/mysql_install_db --user=mysql #以mysql用户的身份初始化数据
[root@leaf mysql]
# chown -R root . #修改mysql目录下的所有文件的属主为root
[root@leaf mysql]
# chown -R mysql data #修改mysql目录下的data目录的属主为mysql
|
1
2
3
4
5
|
[root@leaf mysql]
# bin/mysqld_safe --user=mysql &
[1] 30877
[root@leaf mysql]
# 160306 11:58:50 mysqld_safe Logging to '/var/log/mysqld.log'.
160306 11:58:50 mysqld_safe Starting mysqld daemon with databases from
/var/lib/mysql
160306 11:58:51 mysqld_safe mysqld from pid
file
/var/run/mysqld/mysqld
.pid ended
|
1
2
3
4
5
|
[root@leaf ~]
# tail -f /var/log/mysqld.log
......
2016-03-06 12:00:36 31231 [ERROR]
/usr/local/mysql/bin/mysqld
: Can
't create/write to file '
/var/run/mysqld/mysqld
.pid' (Errcode: 2 - No such
file
or directory)
2016-03-06 12:00:36 31231 [ERROR] Can
't start server: can'
t create PID
file
: No such
file
or directory
160306 12:00:36 mysqld_safe mysqld from pid
file
/var/run/mysqld/mysqld
.pid ended
|
1
2
3
4
5
|
[root@leaf mysql]
# mkdir /var/run/mysqld
[root@leaf mysql]
# cd /var/run/mysqld/
[root@leaf mysqld]
# touch mysqld.pid #创建mysqld.pid文件
[root@leaf mysqld]
# cd ..
[root@leaf run]
# chown -R mysql mysqld #将mysqld目录的属主设置为mysql
|
1
2
3
4
|
[root@leaf run]
# cd /usr/local/mysql
[root@leaf mysql]
# bin/mysqld_safe --user=mysql
160306 12:12:45 mysqld_safe Logging to
'/var/log/mysqld.log'
.
160306 12:12:45 mysqld_safe Starting mysqld daemon with databases from
/var/lib/mysql
|
1
2
3
4
|
[root@leaf ~]
# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31484
/mysqld
|
1
2
3
4
|
[root@leaf mysql]
# bin/mysqladmin version
bin
/mysqladmin
: connect to server at
'localhost'
failed
error:
'Can'
t connect to
local
MySQL server through socket
'/tmp/mysql.sock'
(2)'
Check that mysqld is running and that the socket:
'/tmp/mysql.sock'
exists!
|
1
|
[root@leaf mysql]
# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@leaf mysql]
# bin/mysqladmin version
bin
/mysqladmin
Ver 8.42 Distrib 5.6.29,
for
linux-glibc2.5 on x86_64
Copyright (c) 2000, 2016, 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.
Server version 5.6.29
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket
/tmp/mysql
.sock
Uptime: 6 min 36 sec
Threads: 1 Questions: 2 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.005
|
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
[root@leaf mysql]
# bin/mysqladmin -u root shutdown #通过mysqladmin关闭mysql服务
[root@leaf mysql]
# bin/mysqld_safe --user=mysql & #启动mysql服务
#查看mysql数据库中默认存在的数据库
[root@leaf mysql]
# bin/mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
|
test
|
+--------------------+
#查看mysql数据库(注意此mysql数据库是一个实体,与上面的统称不同)中的数据表
[root@leaf mysql]
# bin/mysqlshow mysql
Database: mysql
+---------------------------+
| Tables |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
#查看mysql数据库中的所有user表
[root@leaf mysql]
# bin/mysql -e "SELECT User, Host, plugin FROM mysql.user" mysql
+------+-----------+-----------------------+
| User | Host | plugin |
+------+-----------+-----------------------+
| root | localhost | mysql_native_password |
| root | leaf | mysql_native_password |
| root | 127.0.0.1 | mysql_native_password |
| root | ::1 | mysql_native_password |
| | localhost | mysql_native_password |
| | leaf | mysql_native_password |
+------+-----------+-----------------------+
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@leaf ~]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 7
Server version: 5.6.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql>
select
User, Host, Password from mysql.user;
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | leaf | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | leaf | |
+------+-----------+----------+
6 rows
in
set
(0.00 sec)
|
-
使用set password语句
-
使用update语句
-
使用mysqladmin命令
1
2
3
4
5
6
|
[root@leaf ~]
# mysql -u root
mysql>
set
password
for
'root'
@
'localhost'
= password(
'123456'
);
mysql>
set
password
for
'root'
@
'127.0.0.1'
= password(
'123456'
);
mysql>
set
password
for
'root'
@
'::1'
= password(
'123456'
);
mysql>
set
password
for
'root'
@
'leaf'
= password(
'123456'
);
mysql> flush privileges;
|
1
2
3
4
|
[root@leaf ~]
# mysql -u root
mysql> update mysql.user
set
password = password(
'123456'
)
-> where User =
'root'
;
mysql> flush privileges;
|
1
|
[root@leaf ~]
# mysqladmin -u root password '123456'
|
1
2
3
4
|
[root@leaf ~]
# mysql -u root
ERROR 1045 (28000): Access denied
for
user
'root'
@
'localhost'
(using password: NO)
[root@leaf ~]
# mysql -u root -p
Enter password:
|
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 |
| | localhost | |
| | leaf | |
+------+-----------+-------------------------------------------+
6 rows
in
set
(0.00 sec)
|
1
2
3
4
|
[root@leaf ~]
# mysql -u root -p
Enter password:
mysql> update mysql.user
set
password = password(
'123456'
) where User =
''
;
mysql> flush privileges;
|
1
2
3
4
|
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> DROP USER
''
@
'localhost'
;
mysql> DROP USER
''
@
'host_name'
;
|
1
2
3
4
|
[root@leaf ~]
# mysql -u root -p
mysql> delete from mysql.db where db like
'test%'
;
mysql> drop database
test
;
mysql> flush privileges;
|