mysql基本操作

 

 

(一)基本操作


1、创建用户使用的数据库
mysql -u root -p
#然后输入密码
mysql> create database filter_conf;
Query OK, 1 row affected (0.08 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| datang2 |
| dt2designer |
| dt2log |
| filter_conf |
| mysql |
| performance_schema |
| test |
+--------------------+
8 rows in set (0.04 sec)


2、创建用户
create user lujinhong identified by 'lujinhong';
grant select,insert,update,delete on *.* to lujinhong@"%" Identified by "lujinhong";
#将所有数据库所有表(*.* )的上述4个权限赋予任何机器上的lujinhong用户(lujinhong@"%")
flush privileges;

3、允许远程连接
Grant all privileges on *.* to 'lujinhong'@'%' identified by 'lujinhong' with grant option;
#其实和上面重复了,给了更多权限
flush privileges;

修改/etc/mysql/my.cnf

bind-address        = 127.0.0.1

改为 0.0.0.0


4、远程连接
/usr/local/mysql/bin/mysql -h 192.168.172.98 -u lujinhong -p
use filter_conf

5、创建表
create table if not exists ma30_conf(property_key varchar(256) primary key, property_value varchar(256) not null, description varchar(1024));

6、向表中插入数据
insert into ma30_conf(property_key,property_value) values("project","ma30");

select * from ma30_conf;

 

 

 

 

 

(三)开启远程连接

若在服务器本地可以连接mysql,但远程连接不行,请参考以下内容

1、首先在服务器上开启mysql服务

 

2、其次确保网络正常,分别

ping ip 以及

telnet ip port(默认是3306)

 

3、连接数据库

mysql  -h ip -u mysql -p

 

事实上,mysql默认是不允许远程连接的,因此可以通过以下方式开启:

1、在服务器上登陆mysql

mysql -h localhost-u mysql -p 
Enter password:    ******

连接数据库。

mysql> use mysql;   (此DB存放MySQL的各种配置信息)
Database changed

2、查看当前的连接权限
mysql> select host,user from user; (查看用户的权限情况)
+-------------+-------+
| host            | user    |
+-------------+-------+
| localhost      |           |
| localhost      | root   |
| localhost      |           |
| localhost      | mysql |
+-------------+-------+
6 rows in set (0.02 sec)

由此可以看出,只能以localhost的主机方式访问。

 

3、新增权限

mysql> Grant all privileges on *.* to 'root'@'%' identified by ‘password’with grant option;
(%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名;‘root’则是指要使用的用户名,里面的password需要自己修改成root的密码)

mysql> flush privileges;  (运行为句才生效,或者重启MySQL)
Query OK, 0 rows affected (0.03 sec)

 

4、再次查看用户的权限情况

mysql> select host,user from user; ()
+-------------+-------+
| host            | user    |
+-------------+-------+

| %                 | mysql |

| %                 | root   |
| localhost      |           |
| localhost      | root   |
| localhost      |           |
| localhost      | mysql |
+-------------+-------+

mysql>exit

 

5、可以在远程机器上连接了。

posted @ 2015-07-16 10:58  lujinhong2  阅读(218)  评论(0编辑  收藏  举报