安裝
參考來自
官方參考
1. sudo apt-get install mysql-client-core-5.6
2.sudo apt-get install mysql-server
3. sudo apt-get install mysql-client
4. 啓動mysql: 安裝後默認啓動,也可用下面的命令操作;
The MySQL server is started automatically after installation. You can check the status of the MySQL server with the following command:
shell> sudo service mysql status
Stop the MySQL server with the following command:
shell> sudo service mysql stop
To restart the MySQL server, use the following command:
shell> sudo service mysql start
5.檢測是否成功: sudo service mysql status
或者sudo netstat -tap | grep mysql
, 輸出如下:
connector
connector有對應多種語言,C++, python等, 相關官方網址如下:
- 官網下載最新tar.gz文件:
mysql-connector-c++-1.1.7-linux-glibc2.5-x86-64bit.tar.gz - 解壓:
tar -zxvf mysql-connector-c++-1.1.7-linux-glibc2.5-x86-64bit.tar.gz
進入解壓後的文件夾, 複製lib和頭文件到相應的文件夾:
sudo cp lib/* /usr/lib/ sudo cp -R include/* /usr/include/
由於mysql-connection.h包含了boost庫,所以需要先安裝boost庫:
sudo apt-get install libboost-all-dev
- 程序參考官網的程序:example
直接編譯會有錯誤: “undefined reference to ‘get_driver_instance’” , 使用如下在命令:
g++ -o test -Iinclude -Llib -L/usr/mysql -lmysqlcppconn test.cpp -lmysqlcppconn
mysql基本命令使用
增加用戶:
- 只能在本地登錄:
grant all on *.* to username@"localhost" identified by "userpasswd";
- 網絡登錄:
grant select,insert,update,delete on *.* to username@"%" identified by "userpasswd";
注:如果不要密碼則直接密碼:”“
- 只能在本地登錄:
顯示當前所有數據庫:
show databases;
- 創建數據庫:
create database 數據庫名;
輸入中文的話需要指定編碼:
CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci
刪除數據庫
drop dbname
drop database if exists dbname
顯示當前使用的數據庫
mysql> select database();
顯示當前數據庫中登陸的用戶
mysql> select user();
顯示當前數據庫中有哪些數據表
mysql> use 數據庫名; mysql> show tables;
建立數據表
mysql> use 數據庫名; mysql> create table 表名 (字段名 integer,字段名 varchar(30), 字段名 char(1)); //如 create table useriinfo(userno integer auto_increment not null primary key,name varchar(32),age integer);
顯示當前數據庫中某個數據表的結構
describe 表名;
向某個表中插入記錄
insert into 表名 values (1,"xxx",18);
顯示某個表中的記錄
mysql> select * from 表名;
指定列的別名查詢
mysql> select fname as '姓名' from student;
更新表中數據
mysql-> update 表名 set 字段名1='x',字段名2='y' where 字段名3='z';
用.sql文件導入數據庫中的表結構
mysql> use 數據庫名; mysql> source .sql文件路徑;
用文本方式將數據裝入一個數據表中:
mysql> load data local infile "文本路徑" into table 表名;
例:
student.txt
1 Sunrier 22
2 Tom 23
3 Jerry 23
列之間使用TAB鍵分割(只能一個TAB鍵),null值用\N來代替(注:N爲大寫字母),數據和表結構對應將表中記錄清空
mysql> delete from 表名;
刪除數據庫中的某個表
mysql> drop table 表名;
命令行修改root密碼:
mysql> update mysql.user set password=password('新密碼') where user='root'; mysql> flush privileges;
顯示有哪些線程在運行
mysql> show processlist;
修改MySQL目錄步驟:
MySQL默認的數據文件存儲目錄爲/var/lib/mysql。假如要放到/home目錄的sunrier/data下,如/home/sunrier/data下需要進行下面幾步:
1)在/home/sunrier目錄下建立data目錄(此時我的home目錄下的sunrier目錄已經存在)
[root@localhost ~]# cd /home/sunrier
[root@localhost sunrier]# mkdir data2)把MySQL服務進程停掉:
[root@localhost sunrier]# service mysqld stop3)把/var/lib/mysql整個目錄移到/home/sunrier/data
[root@localhost sunrier]# mv /var/lib/mysql /home/sunrier/data
[root@localhost sunrier]#4)查找/etc/my.cnf配置文件
如果/etc/目錄下沒有my.cnf配置文件,到/usr/share/mysql/下找到my-medium.cnf文件,拷貝其中一個到/etc/並改名爲my.cnf。
命令如下:
[root@localhost sunrier]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
如果在/usr/share/mysql/下沒有my-medium.cnf.cnf文件,你可以用命令查找下my-medium.cnf文件所在位置,然後拷貝過去[root@localhost sunrier]# find / -name my-medium.cnf /usr/share/doc/mysql-server-5.0.22/my-medium.cnf [root@localhost sunrier]# cp /usr/share/doc/mysql-server-5.0.22/my-medium.cnf /etc/my.cnf [root@localhost sunrier]#
5)修改MySQL的配置文件/etc/my.cnf
爲保證MySQL能夠正常啓動工作,需要指明mysql.sock文件的產生位置.修改socket=/var/lib/mysql/mysql.sock一行中等號右邊的值爲:/home/sunrier/data/mysql/mysql.sock
操作如下:
[mysqld]#socket=/var/lib/mysql/mysql.sock socket=/home/sunrier/data/mysql/mysql.sock
修改數據存放路徑爲實際當前路徑
#datadir=/var/lib/mysql datadir=/home/sunrier/data/mysql
6)重新啓動MySQL服務
[root@localhost sunrier]#service mysqld restart
或用reboot命令重啓Linux