MySQL - 日常操作一 增删改查
mysql 源码安装
创建 mysql 账户 组
groupadd mysql useradd mysql -g mysql -M -s /bin/false
解压缩源码安装
tar zxvf mysql-5.0.22.tar.gz cd mysql-5.0.22 ./configure --prefix=/usr/local/mysql \ --with-client-ldflags=-all-static \ --with-mysqld-ldflags=-all-static \ --with-mysqld-user=mysql \ --with-extra-charsets=all \ --with-unix-socket-path=/var/tmp/mysql.sock make && make install # 生成mysql用户数据库和表文件,在安装包中输入 scripts/mysql_install_db --user=mysql vi ~/.bashrc export PATH="$PATH: /usr/local/mysql/bin"
mysql 文件配置
# 配置文件,有large,medium,small三个,根据机器性能选择 cp support-files/my-medium.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld cd /usr/local chmod 750 mysql -R chgrp mysql mysql -R chown mysql mysql/var -R cp /usr/local/mysql/libexec/mysqld mysqld.old ln -s /usr/local/mysql/bin/mysql /sbin/mysql ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin ln -s -f /usr/local/mysql/bin/mysqld_safe /etc/rc.d/rc3.d/S15mysql5 ln -s -f /usr/local/mysql/bin/mysqld_safe /etc/rc.d/rc0.d/K15mysql5
mysql 常用执行命令
/mysql/bin/mysqld_safe --user=mysql & # 启动mysql服务 ./mysql/bin/mysqladmin -uroot -p -S ./mysql/data/mysql.sock shutdown # 停止mysql服务 mysqlcheck -uroot -p -S mysql.sock --optimize --databases account # 检查、修复、优化MyISAM表
mysqlbinlog slave-relay-bin.000001 # 查看二进制日志(报错加绝对路径)
mysqladmin -h myhost -u root -p create dbname # 创建数据库
mysql 内部常用操作
flush privileges; # 刷新 show databases; # 显示所有数据库 use dbname; # 打开数据库 show tables; # 显示选中数据库中所有的表 desc tables; # 查看表结构 drop database name; # 删除数据库 drop table name; # 删除表 create database name; # 创建数据库 select 列名称 from 表名称; # 查询 show grants for repl; # 查看用户权限 show processlist; # 查看mysql进程 select user(); # 查看所有用户 show slave status\G; # 查看主从状态 show variables; # 查看所有参数变量 show table status # 查看表的引擎状态 drop table if exists user # 表存在就删除 create table if not exists user # 表不存在就创建 select host,user,password from user; # 查询用户权限 先use mysql create table ka(ka_id varchar(6),qianshu int); # 创建表 SHOW VARIABLES LIKE 'character_set_%'; # 查看系统的字符集和排序方式的设定 show variables like '%timeout%'; # 查看超时(wait_timeout) delete from user where user=''; # 删除空用户 delete from user where user='sss' and host='localhost' ; # 删除用户 ALTER TABLE mytable ENGINE = MyISAM ; # 改变现有的表使用的存储引擎 SHOW TABLE STATUS from 库名 where Name='表名'; # 查询表引擎 CREATE TABLE innodb (id int, title char(20)) ENGINE = INNODB # 创建表指定存储引擎的类型(MyISAM或INNODB) grant replication slave on *.* to '用户'@'%' identified by '密码'; # 创建主从复制用户 ALTER TABLE player ADD INDEX weekcredit_faction_index (weekcredit, faction); # 添加索引 alter table name add column accountid(列名) int(11) NOT NULL(字段不为空); # 插入字段 update host set monitor_state='Y',hostname='xuesong' where ip='192.168.1.1'; # 更新数据
自增表
create table oldBoy (id INTEGER PRIMARY KEY AUTO_INCREMENT, name CHAR(30) NOT NULL, age integer , sex CHAR(15) ); # 创建自增表 insert into oldBoy(name,age,sex) values(%s,%s,%s) # 自增插入数据