Mysql默认其他机器不能远程连接数据库,因此,需要设置开启mysql远程连接。
1、首先以root账号登陆mysql
[root@test1 local]# mysql -u root –p |
2、 授权远程连接账号(改表法)
mysql>use mysql; mysql>select 'host' from user where user='root'; mysql>update user set host = '%' where user ='root'; //若提示违反唯一键约束,可以不管 mysql>flush privileges; mysql>select 'host' from user where user='root'; |
离线安装时,遇以下异常:
mysql> use mysql; ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> SET PASSWORD = PASSWORD('tyxj456cg'); //设置密码 Query OK, 0 rows affected (0.00 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> |
意思为,在mysql库中的user表中新增一条记录为host=“%”,user=”root”的数据。
使用以上命令可以会报错,当报错时不用理会,执行下面一步。提示如下:
查询数据库中是否增加成功:
mysql>select host,user,password from user where user='root'; +-----------+------+----------+ | host | user | password | +-----------+------+----------+ | % | root | | | 147 | root | | | 127.0.0.1 | root | | +-----------+------+----------+ |
若显示结果中有 |% | root | |则表示数据增加成功了。
如下图:
看到数据增加成功后,退出mysql重启mysql服务
mysql> exit
使用以下命令重启mysql:
[root@localhost etc]# /etc/init.d/mysql restart Shutting down MySQL...... [ OK ] Starting MySQL.................... [ OK ] |
其他电脑则可以使用工具以root用户账号登陆mysql了。
注:若开启远程后,本地密码连接失败,解决方法参见:
MySQL远程客户端进入MySQL库的user表,将Host为非“%”的其他数据全部删除;并重启数据库,重新加载配置。
如下图: