mysql安装
sudo apt-get install update
sudo apt-get install mysql-server libmysqlclient-dev mysql-workbench
mysql启动/重启/停止
sudo service mysql start/restart/stop
登录mysql命令:mysql -uroot -p
错误异常:
解决办法:
修改mysqld.cnf配置文件
在ubuntu的terminal(也即终端)上输入sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf,进入到这个配置文件,然后在这个配置文件中的[mysqld]这一块中加入skip-grant-tables这句话。
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-grant-tables
作用:就是让你可以不用密码登录进去mysql。
保存,退出。输入:service mysql restart,重新启动mysql。
重新登录设置密码。
代码
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
///
/// \brief sql_create 创建数据库
///
void sql_create()
{
MYSQL mysql;
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, "localhost", "root", "root", "mysql", 3306, NULL, 0)) {
cout<<"mysql connect error: "<<mysql_error(&mysql)<<" "<<mysql_errno(&mysql)<<endl;
}
string str = "create database school;";
mysql_real_query(&mysql, str.c_str(), str.size());
str = "alter database school charset=utf8;";
mysql_real_query(&mysql, str.c_str(), str.size());
// 自动生成id add数据时去掉id
// str = "create table school.students(id int(10) primary key auto_increment, name varchar(20) not null, age int(3) not null);";
//手动添加id
str = "create table school.students(id int(10) primary key , name varchar(20) not null, age int(3) not null);";
mysql_real_query(&mysql, str.c_str(), str.size());
mysql_close(&mysql);
}
///
/// \brief sql_add 增加数据
///
void sql_add()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
if(!mysql_real_connect(&mysql, "localhost", "root", "root", "school", 3306, NULL, 0)) {
cout<<"mysql connect error: "<<mysql_error(&mysql)<<" "<<mysql_errno(&mysql);
}
string str = "insert into students(id, name, age) values(1, \'张三\', 20)";
mysql_real_query(&mysql, str.c_str(), str.size());
str = "insert into students(id, name, age) values(2, \'李四\', 18)";
mysql_real_query(&mysql, str.c_str(), str.size());
str = "insert into students(id, name, age) values(3, \'王五\', 19)";
mysql_real_query(&mysql, str.c_str(), str.size());
str = "insert into students(id, name, age) values(4, \'赵六\', 21)";
mysql_real_query(&mysql, str.c_str(), str.size());
str = "insert into students(id, name, age) values(5, \'马七\', 20)";
mysql_real_query(&mysql, str.c_str(), str.size());
mysql_close(&mysql);
}
///
/// \brief sql_modify 修改数据
///
void sql_modify()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
if(!mysql_real_connect(&mysql, "localhost", "root", "root", "school", 3306, NULL, 0)) {
cout<<"mysql connect error: "<<mysql_error(&mysql)<<" "<<mysql_errno(&mysql);
}
string str = "update students set age = 30 where name = \'王五\'";
mysql_real_query(&mysql, str.c_str(), str.size());
mysql_close(&mysql);
}
///
/// \brief sql_delete 删除数据
///
void sql_delete()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
if(!mysql_real_connect(&mysql, "localhost", "root", "root", "school", 3306, NULL, 0)) {
cout<<"mysql connect error: "<<mysql_error(&mysql)<<" "<<mysql_errno(&mysql);
}
string str = "delete from students where name = \'赵六\'";
mysql_real_query(&mysql, str.c_str(), str.size());
mysql_close(&mysql);
}
///
/// \brief sql_query 查询数据
///
void sql_query()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
if(!mysql_real_connect(&mysql, "localhost", "root", "root", "school", 3306, NULL, 0)) {
cout<<"mysql connect error: "<<mysql_error(&mysql)<<" "<<mysql_errno(&mysql);
}
string str = "select * from students;";
mysql_real_query(&mysql, str.c_str(), str.size());
MYSQL_RES *result = mysql_store_result(&mysql);
MYSQL_ROW row;
while (row = mysql_fetch_row(result)) {
cout<<"id: "<<row[0]<<" name: "<<row[1]<<" age: "<<row[2]<<endl;
}
mysql_free_result(result);
mysql_close(&mysql);
}
int main()
{
cout << "Hello World!111" << endl;
sql_create();
cout << "Hello World!222" << endl;
sql_add();
cout << "Hello World!333" << endl;
sql_query();
cout << "Hello World!444" << endl;
sql_modify();
cout << "Hello World!555" << endl;
sql_query();
cout << "Hello World!666" << endl;
sql_delete();
cout << "Hello World!777" << endl;
sql_query();
return 0;
}
编译:
g++ main.cpp -lmysqlclient