mysql的基础,我们之前已经学过,后面我们只关心使用
要使用C语言连接mysql,需要使用mysql官网提供的库,大家可以去官网下载
我们使用C接口库来进行连接
要正确使用,我们需要做一些准备工作:
- 保证mysql服务有效
- 在官网上下载合适自己平台的mysql connect库,以备后用
[root@iZ5waahoxw3q2bZ ~]# mysql -uroot -p
Enter password:
输入之前设置的密码123456
1.简单创建
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> select User,Host from user;
+—————+———–+
| User | Host |
+—————+———–+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+—————+———–+
3 rows in set (0.00 sec)
如果发现还有其他用户,比如zhangsan的可以用如下的
mysql> drop user 'zhangsan'@'%';
建立一个本地用户
mysql> create user 'connector'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> select User,Host from user;
+—————+———–+
| User | Host |
+—————+———–+
| connector | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+—————+———–+
4 rows in set (0.00 sec)
登陆我们新创建的本地的connector
终端1
mysql> create user 'connector'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> select User,Host from user;
+—————+———–+
| User | Host |
+—————+———–+
| connector | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+—————+———–+
4 rows in set (0.00 sec)
终端2
[root@iZ5waahoxw3q2bZ ~]# mysql -uconnector -p
Enter password:
mysql>
创建一个conn数据库
终端1
mysql> create database conn;
Query OK, 1 row affected (0.00 sec)
给connector这个conn数据库所有权限
终端1
mysql> grant all on conn.* to 'connector'@'localhost';
Query OK, 0 rows affected (0.00 sec)
刷新一下权限
终端1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
终端2
刷新前
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
+——————–+
1 row in set (0.00 sec)
刷新后
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| conn |
+——————–+
2 rows in set (0.00 sec)
mysql> use conn;
Database changed
mysql> show tables;
Empty set (0.00 sec)
我们发现用Windows的终端去连接,连接不上,是因为我们设置的是localhost
C:\\Users\\wozha>mysql -uconnector -h121.40.196.90 -p 8080 -p
Enter password: ******
ERROR 2003 (HY000): Can't connect to MySQL server on '121.40.196.90:3306' (10060)
如果想要连接可以更改Host。
感谢你的观看,期待我们下次再见!

