1 准备工作3
2 安装Mysql需要的依赖3
2.1 安装 Mysql3
3 常用命令3
3.1 验证原有主机是否已安装3
3.2 启动4
3.3 登录4
3.4 远程登录4
3.5 重启服务4
3.6 停止服务4
3.7 查看服务状态4
4 错误解决方案4
本文档是在ubuntu14.04下安装5.6版本的mysql。
1.所用软件:
Ubuntu14.04 LTS版
2.建议使用root用户
3.使用服务器地址:192.168.1.236
4.安装mysql5.6.19
安装mysql需要依赖,不然启动失败
apt-get install mysql-client-core-5.6
apt-get install mysql-client-5.6
如果运行如上命令后,在命令行信息最后看到类似
apt-get install mysql-server-5.6
安装过程中输入root的密码即可;
这里主要是运行sudo netstat -tap | grep mysql命令查看是否有MySQL的端口,如果不加sudo的话因为权限无法顺利执行:
netstat -tap | grep mysql
如果按照成功,执行上面命令会显示:
tcp 0 0 localhost:mysql *:* LISTEN 6840/mysqld
# 启动MySQL$ sudo service mysql start # 关闭MySQL$ sudo service mysql stop# 重启MySQL$ sudo service mysql restart# 其他命令:$ sudo /etc/init.d/mysql start$ sudo /etc/init.d/mysql stop$ sudo /etc/init.d/mysql restart
然后,再次键入命令来检查是否已启动MySQL:
$ netstat -tap | grep mysql
MySQL监听在localhost,说明MySQL已经启动。
使用命令行mysql -u root -p 来登录MySQL:
$ mysql -u root -p
$ mysql -h192.168.1.160 -u root -p
sudo service mysql restart
sudo service mysql stop
sudo service mysql status
from:http://my.oschina.net/ydsakyclguozi/blog/402048
以下是摘录的网络上的安装tar的mysql的文件,共借鉴是用。
http://www.cnblogs.com/gaojupeng/p/5727069.html
由于公司临时让将Oracle的数据移植到mysql上面,所以让我在公司服务器上面安装一下mysql。下面就是我的安装过程以及一些错误解决思路。其实对于不同版本安装大体都有差不多。
mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
官网: http://dev.mysql.com/downloads/mysql/
1
2
|
#rpm -qa|grep -i mysql
mysql-5.7.13-linux-glibc2.5-x86_64
|
*可见已经安装了库文件,应该先卸载,不然会出现覆盖错误。注意卸:载时使用了--nodeps选项,忽略了依赖关系:
1
|
#rpm -e mysql-5.7.13-linux-glibc2.5-x86_64
--nodeps
|
当然你的也可能不止这一个文件,也可能有多个,那么你就依次 rpm -e xx --nodeps 卸载,卸载完了再检查一下,若确定删除干净后,在经行下面步骤。
在这步之前一定要先确保你所建的用户以及用户组没有存在,要不然在后面的过程中会报错,删除时候要先删除用户在删除用户组名。
1
2
3
4
5
6
|
# userdel mysql # 删除用户
# groupdel mysql # 删除用户组名
# mkdir /home/mysql # 在home文件夹下创建文件夹mysql
# mkdir /home/mysql/data # 在mysql文件夹下创建文件夹data
# groupadd mysql # 创建一个名为mysql的用户组
# useradd -g mysql -d /home/mysql mysql # 在用户组下创建用户
|
1
2
3
|
# tar -xzvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz # 解压文件
# cd mysql-5.7.13-linux-glibc2.5-x86_64 # 进入
# mv * /home/mysql # 移动到之前我创建的mysql文件夹下。
|
1
2
3
4
5
6
7
8
|
# cd /home/mysql # 进入安装目录
# ./bin/mysql_install_db
--user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
[WARNING] mysql_install_db
is
deprecated. Please consider switching
to
mysqld
--initialize 2015-11-10 02:09:23
[WARNING] The bootstrap log isn't empty: 2015-11-10 02:09:23
[WARNING] 2015-11-10T10:09:18.114182Z 0
[Warning]
--bootstrap is deprecated. Please consider using --initialize instead 2015-11-10T10:09:18.129343Z 0
[Warning] Changed limits: max_open_files: 1024 (requested 5000) 2015-11-10T10:09:18.129408Z 0
[Warning] Changed limits: table_open_cache: 431 (requested 2000)
|
mysql5.7新特性:由上面可以看出, mysql_install_db
已经不再推荐使用了,建议改成 mysqld --initialize
完成实例初始化。
1
2
3
4
5
|
# ./bin/mysqld
--user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
# ./mysqld
--user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
[Warning]
TIMESTAMP
with
implicit
DEFAULT
value
is
deprecated. Please use
--explicit_defaults_for_timestamp server option (see documentation for more details).
[ERROR]
--initialize specified but the data directory has files in it. Aborting. 2016-04-08T01:46:53.155879Z 0
[ERROR] Aborting
|
出现上面的错误是因为我们没有清空mysql的data目录, 执行清空命令如下:
1
2
3
4
5
6
7
8
9
|
# cd /home/mysql/data # 进入安装目录下的data目录
# rm -fr * # 清空数据
# cd /home/mysql
# ./bin/mysqld
--user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
[Warning]
TIMESTAMP
with
implicit
DEFAULT
value
is
deprecated. Please use
--explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0
[Warning] InnoDB: New log files created, LSN=45790 2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating
foreign
key
constraint
system tables. 2016-04-08T01:48:00.434908Z 0
[Warning]
No
existing UUID has been found, so we assume that this
is
the
first
time
that this server has been started. Generating a new UUID: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0
[Warning] Gtid
table
is
not
ready
to
be used.
Table
'mysql.gtid_executed'
cannot be opened. 2016-04-08T01:48:00.440904Z 1
[Note] A
temporary
password
is
generated
for
root@localhost: **mjT,#x_5sW
|
牢记上面的随机密码, 如上**mjT,#x_5sW
, 下面我们修改密码时需要用到。
1
2
3
|
# cd /home/mysql
# ./support-files/mysql.server start
Starting MySQL.. OK!
|
这是正常的情况下。
刚刚我用的是/home/mysql
为mysql的安装目录basedir, 则在启动服务时会出现如下错误:
1
2
3
|
# ./support-files/mysql.server start
./support-files/mysql.server: line 276: cd: /usr/
local
/mysql:
No
such file
or
directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/
local
/mysql/bin/mysqld_safe)
|
由上面可知mysql的tar.gz安装包的默认安装目录为/usr/local/mysql
, 这时候我们需要修改/support-files/mysql.server
文件的basedir和datadir目录路径为我们环境所在的mysql的basedir和datadir路径, 如下:
1
2
3
4
5
6
7
8
9
|
# vim support-files/mysql.server
--------------------------
...
basedir=/home/mysql
datadir=/home/mysql/data
...
--------------------------
# ./support-files/mysql.server start
start Starting MySQL.. OK!
|
注意:上面
basedir 以及datadir部分是手动改的,那么在命令中如何手动改,步骤
1、 按 键盘 i 键(insert),然后就可以在你想要的位置编写了。 2、编辑完毕退出,esc键。 3、Shift键 加 :(冒号键)退出编辑。
4、wq! 保存编辑
1
|
# ln -s /home/mysql/bin/mysql /usr/bin/mysql
|
下面的前3步操作过程中,可能会出错,如果有错就跳过,因为不同电脑的环境不一样,
1、将默认生成的my.cnf备份
1
|
# mv /etc/my.cnf /etc/my.cnf.bak
|
如果这一步报错,没有关系,正常情况下是由于/ect文件夹下没有my.cnf锁引起的,创建一下就可以了,或者你不会创建 , 直接使用 vim /ect/my.cnf 命令编辑 , 然后直接退出编辑就可以,在执行这个命令就不会出错了,退出编辑命令 esc键 -> (shift键+ :(冒号键) -> wq! ),然后在执行上面的命令就不会报错了。 2、进入mysql的安装目录支持文件目录
1
|
# cd /home/mysql/support-files
|
3、拷贝配置文件模板为新的mysql配置文件,
1
|
# cp my-
default
.cnf /etc/my.cnf
|
4、设置编码,可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行.
如下是我修改配置文件/etc/my.cnf, 设置编码为utf8以防乱码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# vim /etc/my.cnf
[mysqld]
basedir = /home/mysql
datadir = /home/mysql/data
character_set_server=utf8
init_connect=
'SET NAMES utf8'
[client]
default
-
character
-
set
=utf8
|
有的时候使用这个命令后,前3步有问题的这里可能my.cnf文件是空的,这个不要紧,既然没有生成,那么你就自己编辑喽!具体编辑怎么实现就参照步骤6.
1
2
3
4
5
|
# cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld # 拷贝启动文件到/etc/init.d/下并重命令为mysqld
# chmod 755 /etc/init.d/mysqld # 增加执行权限
# chkconfig
--list mysqld # 检查自启动项列表中没有mysqld这个,
# chkconfig
--add mysqld # 如果没有就添加mysqld:
# chkconfig mysqld
on
# 用这个命令设置开机启动:
|
如果在执行第一步就报错,则说明刚刚在第8步中配置出问题了,那你就重新设置一下
做完这步后再重新执行下上面的操作 如果后面的操作中出现以下的问题
这样就是电脑环境的问题了,你就直接把
1
2
3
|
# service mysqld start # 启动服务
# service mysqld restart # 重启服务
# service mysqld stop # 停止服务
|
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
|
# cd /home/mysql
# ./bin/mysqladmin -u root -p
'**mjT,#x_5sW'
password
'123456'
mysqladmin: # 刚刚之前的初始密码
[Warning] Using a
password
on
the command line interface can be insecure.
Warning: Since
password
will be sent
to
server
in
plain text, use ssl
connection
to
ensure
password
safety.
mysql> use mysql
Database
changed
mysql>
update
user
set
authentication_string =
PASSWORD
(
'123456'
)
where
user
=
'root'
;
Query OK, 0
rows
affected, 1 warning (0.00 sec)
Rows
matched: 2 Changed: 0 Warnings: 1
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.13,
for
linux-glibc2.5 (x86_64) using EditLine wrapper
Connection
id: 3
Current
database
: mysql
Current
user
: root@localhost
SSL:
Not
in
use
Current
pager: stdout
Using outfile:
''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection
: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 1
hour
29
min
17 sec
Threads: 1 Questions: 50 Slow queries: 0 Opens: 136 Flush tables: 1
Open
tables: 129 Queries per
second
avg
: 0.009
--------------
|
如果这一步完成不了的话,也就是修改不了密码的话,你也可以直接下面的方式先登录
如果上面两步都完成不了
1
2
3
4
|
root@s0:/home/mysql# ./bin/mysqladmin -u root -p
'946//EE=v!Ne'
password
'123456'
mysqladmin:
Enter password:
mysqladmin: connect to server at
'localhost'
failed --不让你修改
error:
'Access denied for user '
root
'@'
localhost
' (using password: YES)'
|
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
|
root@s0:/home/mysql# find / -name mysql.sock
/tmp/mysql.sock
root@s0:/home/mysql# /home/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
Enter password:
ERROR 1045 (28000): Access denied
for
user
'root'
@
'localhost'
(
using
password: NO) --输入之前的密码
root@s0:/home/mysql# /home/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id
is
3
Server version: 5.7.13
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.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to clear the current input statement.
mysql>
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password
using
ALTER USER statement before executing
this
statement. --没有权限
mysql> alter user
'root'
@
'localhost'
identified
by
'root'
;
Query OK, 0 rows affected (0.04 sec)
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> update user
set
authentication_string = PASSWORD(
'123456'
)
where
user =
'root'
;
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> \s
|
但是如果不修改密码,不进每次登陆麻烦而且在执行一些操作会报一下错误
1
|
You must reset your
password
using
ALTER
USER
statement before executing this statement # 意思就是你必须重置你的密码在执行这个
execute
操作前
|
这里教大家用另外一种方式修改密码,这个是在已经登陆进去的情况下依次执行。
1
2
3
4
5
6
7
8
9
|
mysql>
SET
PASSWORD
=
PASSWORD
(
'123456'
); #
PASSWORD
()里面的123456 是我设置的新密码,你也可以设置成你的密码
Query OK, 0
rows
affected, 1 warning (0.00 sec)
Rows
matched: 2 Changed: 0 Warnings: 1
mysql>
ALTER
USER
'root'
@
'localhost'
PASSWORD
EXPIRE NEVER;
Query OK, 0
rows
affected, 1 warning (0.00 sec)
Rows
matched: 2 Changed: 0 Warnings: 1
mysql> flush
privileges
;
Query OK, 0
rows
affected, 1 warning (0.00 sec)
Rows
matched: 2 Changed: 0 Warnings: 1
|
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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> \s
--------------
/usr/soft/mysql/bin/mysql Ver 14.14 Distrib 5.7.13,
for
linux-glibc2.5 (x86_64)
using
EditLine wrapper
Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not
in
use
Current pager: stdout
Using outfile:
''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: latin1 ---需要修改成你想要的编码
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 27 sec
Threads: 1 Questions: 43 Slow queries: 0 Opens: 136 Flush tables: 1 Open tables: 129 Queries per second avg: 1.592
--------------
mysql> show variables like
'character%'
;
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/soft/mysql/share/charsets/ |
+--------------------------+---------------------------------+
8 rows
in
set
(0.00 sec)
mysql> SET character_set_database = utf8; ----设置编码
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show variables like
'character%'
;
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/soft/mysql/share/charsets/ |
+--------------------------+---------------------------------+
8 rows
in
set
(0.00 sec)
mysql> \s
--------------
/usr/soft/mysql/bin/mysql Ver 14.14 Distrib 5.7.13,
for
linux-glibc2.5 (x86_64)
using
EditLine wrapper
Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not
in
use
Current pager: stdout
Using outfile:
''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 3 min 28 sec
Threads: 1 Questions: 52 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.250
--------------
mysql>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@testfornss ~]# mysql -u root -p
Enter
password
:
Welcome
to
the MySQL monitor. Commands
end
with
;
or
\g.
Your MySQL
connection
id
is
4
Server version: 5.7.13 MySQL Community Server (GPL)
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.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear the
current
input statement.
mysql>
grant
all
privileges
on
*.*
to
'root'
@
'%'
identified
by
'123456'
;
Query OK, 0
rows
affected, 1 warning (0.00 sec)
mysql>
|
或者:
1
|
grant
all
on
*.*
to
'root'
@
'%'
identified
by
'123456'
;
|
如果不设置远程连接,用mysql数据库连接工具可是连接不上的哦!!!
首先要知道,远程连接的Linux系统的ip,确保本机已经能够ping通的情况下再连接。
到这里说明你的mysql安装成功了,是不是感觉特别简单啊!!!!