1,用root账号登陆mysql客户端
mysql -u root -p
2,使用mysql数据库
mysql> use mysql;
3,查看现有的mysql账号
mysql> select user,host from user;
+----------+---------------+
| user | host |
+----------+---------------+
| root | 192.168.1.135 |
| waleking | 192.168.1.135 |
| | bogon |
| root | bogon |
| | localhost |
| root | localhost |
+----------+---------------+
4,添加新的远程账号
grant all on *.* to 'remote'@'192.168.1.104' identified by '123456';
*.* 表示任何数据库上的任何表,all代表任何权限。remote是远程登陆的用户名,192.168.1.104是远程访问的ip,123456是远程访问的remote账号对应的密码。
5, 在mysql命令行下
mysql>flush privileges;
注意:往往以上操作执行之后,仍然不能进行远程访问,这个时候需要执行第6和第7的操作:
6, 修改my.cnf
修改/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.
也就是说默认的mysql是仅仅监听localhost的请求,所以1-5的操作做了半天都看不到效果,很让人沮丧。
所以,我们要将下面这句话
bind-address = 127.0.0.1
修改为你的mysql服务器的ip地址,例如
bind-address = 192.168.1.2
7,重启mysql服务
找到mysqladmin的位置,可以用whereis mysql来找:
mysql: /usr/bin/mysql /etc/mysql /usr/lib/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
这说明mysqladmin的位置在/usr/bin
切换到这个目录,然后执行: sudo mysqladmin -u root -ppwd restart
-ppwd是你的mysql的root账号的密码。
这个操作是关闭mysql服务,一般而言,mysql在关闭后会自动启动。
8,如果1-7的操作还是不行,检查linux防火墙
ubuntu的防火墙ufw,其内核是iptables,可以通过iptables -L -n 查看防火墙的规则。
附注:
几个有用的操作:
(1)检查日志,检查mysql的错误日志是很有效果的。
要找到错误日志,可以在mysql命令行下输入:
mysql>show variables like 'log_%';
+---------------------------------+--------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_bin_trust_routine_creators | OFF |
| log_error | /var/log/mysql/error.log |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_queries | OFF |
| log_warnings | 1 |
+---------------------------------+--------------------------+
说明错误日志在/var/log/mysql/error.log
(2)监听端口
netstat -tln
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.2 :3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
...
看到3306端口的服务正在监听,说明mysql服务正常,另外可以从中看到my.cnf的修改生效。