侧边栏壁纸
博主头像
★街角晚灯★博主等级

博观而约取 厚积而薄发

  • 累计撰写 446 篇文章
  • 累计创建 182 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Win7下利用SQLyog远程访问Ubuntu下MySql数据库

WinJay
2013-05-21 / 0 评论 / 0 点赞 / 143 阅读 / 11954 字 / 正在检测是否收录...
温馨提示:
文章发布较早,内容可能过时,阅读注意甄别。。。。

Win7下利用SQLyog远程访问Ubuntu下MySql数据库

今天下午,部门一同事问我要一台服务器上的MySQL数据库。。。

可是我不知道密码啊。。。怎么破。。。。猜吧。。

首先 那台服务器是台Ubuntu的服务器。。先看看前人有没有配置好远程访问。。

在本地装了一个SQLyog 连接报错。。。

看来是啥也没配置。。。。。过了会儿,同事告诉了我MySql的密码。。。有了密码就好办了。。

远程SHH登录服务器。。。

一边google一边做啊。。。

我用的是下面的第二种方法,首先得进入MySql

root@nerc-svr:~# mysql -u root -p

Enter password:

输入密码后,运行GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@’%’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;

用户名与密码根据实际情况填写。。建议直接复制。。我手动敲了好几次都错了。。。汗。。。

下面是摘自网络上的资料:

MySql远程连接的设置问题

如果使用 web 和数据库不在同一台服务器上,MySQL 服务器就需要允许远程链接,网站才能正常运行。 MySQL 的远程链接设置有两种方法:

  1. 改表法。 可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称”%”
mysql -u root -pvmwaremysql>use mysql;mysql>update user set host = ‘%’ where user = ‘root’;mysql>select host, user from user;
  1. 授权法。 例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
‘myuser’@’192.168.1.3’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@’%’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;

如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码 GRANT ALL PRIVILEGES ON . TO

运行完正常的话会出现“Query OK, 0 rows affected (0.00 sec)” 这样的提示,表示已经授权成功。

授权成功了,还需要将3306端口打开。。要不然远程还是没法访问。。

以下亦是摘自网络。。。

设置Ubuntu上的MySQL可以远程访问

1. 3306端口是不是没有打开?

使用nestat命令查看3306端口状态:

netstat -an | grep 3306

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN

从结果可以看出3306端口只是在IP 127.0.0.1上监听,所以拒绝了其他IP的访问。

解决方法:修改/etc/mysql/my.cnf文件。打开文件,找到下面内容:

# Instead of skip-networking the default is now to listen only on

# localhost which is more compatible and is not less secure.

bind-address = 127.0.0.1

把上面这一行注释掉或者把127.0.0.1换成合适的IP,建议注释掉。

重新启动后,重新使用netstat检测:

netstat -an | grep 3306

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN

2. 问题解决了吗?

现在使用下面命令测试:

~# mysql -h 10.1.1.2 -u root -p

Enter password:

ERROR 1130 (00000): Host ‘B0324-Desktop.local’ is not allowed to connect to this MySQL server

结果出乎意料,还是不行。

解决方法:原来还需要把用户权限分配各远程用户。

登录到mysql服务器,使用grant命令分配权限

mysql> grant all on database_name.* to user_name@’%’ identified by ‘user_password’;

其中database_name、user_name和user_password根据实际情况设置。

完成后使用mysql命令连接,提示成功,为了确保正确可以再远程登陆测试一下。

在做上面的操作的时候我又重新温习了一下VI命令。。。。。Win7下利用SQLyog远程访问Ubuntu下MySql数据库。。

2.2 编辑模式:

a) i, I : 在光标所在处插入输入文字,已存在的文字向后退。i 为‘从当前光标所在处插入’,I 为‘在当前所在行的一个非空格符处开始插入’。
b) a, A : a 为‘从当前光标所在处的下一个字符开始插入’。A 为‘从光标所在行的最后一个字符处开始插入’。
c) o,O : 这是英文o的大小写。o为‘在当前光标所在行的下一行处插入新的一行’。O表示‘在当前光标所在行的上一行插入新的一行’。
d) r,R : 替换:r 会替换光标所在的那一个字符。 R : 会一直替换光标所在的字符,直到按下esc 键为止。
e) ESC : 进入一般模式。

2.3 命令模式:

a) :w : 将编辑的数据写入硬盘
b) :q : 离开vi
c) :q! : 强制离开,不存储
d) :wq : 存储后离开
e) :wq! : 强制存储后离开

可是做完上面的步骤后还是不能通过sqlyog连接呢。。。

于是在本地 telnet了一下,还是3306这个端口有问题。。。。继而想到了防火墙。。。再搜关于防火墙的资料。。。

在网上找了一段命令:

接下来给iptables中添加虽则,是的mysql的3306端口可以被访问到

[root@ubuntu]:# iptables -A INPUT -p tcp –dport 3306 -j ACCEPT

运行后还是连不上。。

继而google出了这一段。。。

http://blog.sina.com.cn/s/blog_8f3de3250100xy1u.html

其中在文章未有一个:UFW 使用范例:

我就照葫芦画瓢。。运行了一下“

允许 80 端口

$ sudo ufw allow 80/tcp

只是将80换成了3306

再次运行/etc/init.d/mysql restart

再用sqlyog连接 成功。。。。

中途同事提供了一段本地备份mysql数据库的命令。。

比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump 命令格式如下:

[root@linuxsir01 root]# mysqldump -u root -p linux > /root/linux.sql
Enter password:		#在这里输入数据库的密码

通过上面的命令,我们要明白两件事,首先备份数据库是要以数据库管理员的身份备份;其次:备份目的地是/root,备份的文件名是linux.sql。其实备份的位置和文件名,根据自己的情况来定。文件名可以自己来取,路径也可以自己来安排;


2013.5.28补记。。

今天对重装的Linux 进行数据库恢复。。搜到了以下命令。。

MySQL命令行导入数据库:

1,将要导入的.sql文件移至bin文件下,这样的路径比较方便 2,同上面导出的第1步 3,进入MySQL:mysql -u 用户名 -p 如我输入的命令行:mysql -u root -p (输入同样后会让你输入MySQL的密码) 4,在MySQL-Front中新建你要建的数据库,这时是空数据库,如新建一个名为news的目标数据库 5,输入:mysql>use 目标数据库名; 如我输入的命令行:mysql>use news; 6,导入文件:mysql>source 导入的文件名; 如我输入的命令行:mysql>source news.sql;

MySQL备份和还原,都是利用mysqldump、mysql和source命令来完成的。

1.Win32下MySQL的备份与还原

1.1 备份

开 始菜单 | 运行 | cmd |利用“cd \Program Files\MySQL\MySQL Server 5.0\bin”命令进入bin文件夹 | 利用“mysqldump

-u 用户名 -p databasename >exportfilename”导出数据库到文件,如mysqldump -u root -p voice>voice.sql,然后输入密码即可开始导出。

1.2 还原

进入MySQL Command Line Client,输入密码,进入到“mysql>”,输入命令”show databases;”,回车,看看有些什么数据库;建立你要还原的数据库,输入”create database voice;”,回车;切换到刚建立的数据库,输入”use voice;”,回车;导入数据,输入”source voice.sql;”,回车,开始导入,再次出现”mysql>”并且没有提示错误即还原成功。

2.Linux下MySQL的备份与还原

2.1 备份

[root@localhost ~]# cd /var/lib/mysql(进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) [root@localhost mysql]# mysqldump -u root -p voice>voice.sql 输入密码即可。

2.2 还原

法一: [root@localhost ~]# mysql -u root -p 回车,输入密码,进入MySQL的控制台”mysql>”,同1.2还原。 法二: [root@localhost ~]# cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) [root@localhost mysql]# mysql -u root -p moodle<moodle_bak.sql 输入密码即可(将要恢复的数据库文件放到服务器的某个目录下,并进入这个目录|moodle_bak.sql是需要恢复的文件名)。

以下是操作的代码及过程。。。

Last login: Sat Apr 27 16:51:39 2013 from 10.191.197.175

root@nerc-svr:~# /etc/init.d/mysql start

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql start

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the start(8) utility, e.g. start mysql

root@nerc-svr:~# mysql -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 26622

Server version: 5.5.29-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2012, 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 ‘{DatabaseNameOrPassword}’ WITH GRANT OPTION;	###


Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

iptables -A INPUT -p tcp –dport 3306 -j ACCEPT; # iptables放行3306端口

root@nerc-svr:~# /etc/init.d/mysql restart

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the stop(8) and then start(8) utilities,

e.g. stop mysql ; start mysql. The restart(8) utility is also available.

mysql stop/waiting

mysql start/running, process 23936

root@nerc-svr:~# mysqldump -u root -p {DatabaseName} > /root/{DatabaseBackupName}.sql //本地备份mysql数据库

Enter password:

root@nerc-svr:~# netstat -nap|grep 3306 //查看3306被允许使用的IP。

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 23936/mysqld



root@nerc-svr:~# vi /etc/mysql/my.cnf

\#

\# The MySQL database server configuration file.

\#

\# You can copy this to one of:

\# – “/etc/mysql/my.cnf” to set global options,

\# – “~/.my.cnf” to set user-specific options.

\#

\# One can use all long options that the program supports.

\# Run program with –help to get a list of available options and with

\# –print-defaults to see which it would actually understand and use.

\#

\# For explanations see

http://dev.mysql.com/doc/mysql/en/server-system-variables.html#

\# This will be passed to all mysql clients

\# It has been reported that passwords should be enclosed with ticks/quotes

\# escpecially if they contain “#” chars…

\# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

[client]

port = 3306

socket = /var/run/mysqld/mysqld.sock

\# Here is entries for some specific programs

\# The following values assume you have at least 32M ram

\# This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]

socket = /var/run/mysqld/mysqld.sock

nice = 0

[mysqld]

\#

\# * Basic Settings

\#

user = mysql

pid-file = /var/run/mysqld/mysqld.pid

socket = /var/run/mysqld/mysqld.sock

port = 3306

basedir = /usr

datadir = /var/lib/mysql

tmpdir = /tmp

lc-messages-dir = /usr/share/mysql

skip-external-locking

\#

\# Instead of skip-networking the default is now to listen only on

\# localhost which is more compatible and is not less secure.

\#bind-address = 127.0.0.1 //进行注释。。

\#

\# * Fine Tuning

\#

key_buffer = 16M

max_allowed_packet = 16M

thread_stack = 192K

“/etc/mysql/my.cnf” 125L, 3505C written

root@nerc-svr:~# /etc/init.d/mysql restart

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the stop(8) and then start(8) utilities,

e.g. stop mysql ; start mysql. The restart(8) utility is also available.

mysql stop/waiting

mysql start/running, process 24376





root@nerc-svr:~# netstat -an | grep 3306

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

root@nerc-svr:~# mysql -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 77

Server version: 5.5.29-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2012, 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 on moodle.* to root@’%’ identified by ‘nerc.nerc/1’;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

root@nerc-svr:~# /etc/init.d/mysql restart

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the stop(8) and then start(8) utilities,

e.g. stop mysql ; start mysql. The restart(8) utility is also available.

mysql stop/waiting

mysql start/running, process 24657

root@nerc-svr:~# sudo ufw allow 3306/tcp //添加3306端口到防火墙规则。

Rule added

Rule added (v6)

root@nerc-svr:~# /etc/init.d/mysql restart

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the stop(8) and then start(8) utilities,

e.g. stop mysql ; start mysql. The restart(8) utility is also available.

mysql stop/waiting

mysql start/running, process 25234

root@nerc-svr:~#
0

评论区