前言:有的时候,我们的数据库在ubantu中,我们需要用navicat进行连接,我们就要先设置一下我们ubantu中的数据库,步骤如下
1、安装数据库
安装mysql-server会弹框提示输入root密码,需要重复确认
sudo apt-get install mysql-server
sudo apt-get install mysql-client
1.1安装成功后可以使用下面命令查看mysq的状态
root@MJ:/home/majun# netstat -tap | grep mysql
tcp 0 0 localhost:mysql *:* LISTEN 1022/mysqld
2、 数据库访问权限设置
//查看当前有那些数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MJ |
| RUNOOB |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
//切换使用mysql数据库
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
//查询user中的数据
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
//# 修改root账号的host字段为:%
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//修改成功
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
//我这里想给任意主机root账户连接mysql服务器权限 但是失败了,
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
//#使用了这个 刷新权限表,使配置生效
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
3、查看ubuntu中3306端口监听: # netstat -anpt | grep 3306
root@MJ:/home/majun# netstat -anpt | grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1022/mysqld
4、修改mysql配置文件,将这个mysql和当前主机ip进行绑定(我理解的是)
sudo vi /etc/mysql/mysql.conf.d/mysqld.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 = 192.168.13.150
#bind-address = 127.0.0.1 //原来是这一行,我注释掉了,改为了我自己的主机ip
按照上面的步骤操作后就可以用navicat尝试着连接数据库了
连接过程中的信息:
连接名字:这个你自己起一个吧(我默认为我自己的主机号,这样方便识别)
主机:填写你刚刚更改的这个ip
端口:之前用netstat查看的mysql监听的端口(一般默认3306)
用户名:root (如果你添加了其他用户的,并且授权成功了的,那用户名和密码就输入你授权的那个,下面一步忽略)
密码:登录这个root的密码
这样navicat就可以登录了你ubantu中的mysql了。
注:如果不想使用密码这些登录,就在
/etc/mysql/mysql.conf.d/mysqld.cnf文件(mysql配置文件)中添加一行 skip-grant-tables
mysql的配置文件中:
27 [mysqld]
28 #
29 # * Basic Settings
30 #
31 user = mysql
32 pid-file = /var/run/mysqld/mysqld.pid
33 socket = /var/run/mysqld/mysqld.sock
34 port = 3306
35 basedir = /usr
36 datadir = /var/lib/mysql
37 tmpdir = /tmp
38 lc-messages-dir = /usr/share/mysql
39 skip-external-locking
40 skip-grant-tables //添加这一行,这样所有的用户就可以不用密码登录了,然后重启mysql就可以