您的位置:首页 > 运维架构 > Linux

linux mysql操作和配置

2016-05-28 15:32 489 查看

安装

参考来自

官方参考

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等, 相关官方网址如下:

http://dev.mysql.com/downloads/connector/

官网下载最新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 data

2)把MySQL服务进程停掉:

[root@localhost sunrier]# service mysqld stop

3)把/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql linux