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"
# 配置文件,有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更改密码{
update user set password=password('passwd') where user='root';
flush privileges;
mysqladmin -u root password 'xuesong'
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 # 创建数据库
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 # 查看表的引擎状态
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(字段不为空); # 插入字段
增加MySQL用户{
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户user_1密码为123,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
mysql> grant select,insert,update,delete on *.* to user_1@"%" Identified by "123";
mysql>flush privileges;
例1增加的用户是十分危险的,如果知道了user_1的密码,那么他就可以在网上的任何一台电脑上登录你的MySQL数据库并对你的数据为所欲为了,解决办法见例2。
例2、增加一个用户user_2密码为123,让此用户只可以在localhost上登录,并可以对数据库aaa进行查询、插入、修改、删除的操作(localhost指本地主机,即MySQL数据库所在的那台主机),
这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过 MYSQL主机来操作aaa库。
mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123";
用新增的用户如果登录不了MySQL,在登录时用如下命令:
mysql -u user_1 -p -h 192.168.113.50 (-h后跟的是要登录主机的ip地址)
}
登录mysql的命令{
# 格式: mysql -h 主机地址 -u 用户名 -p 用户密码
mysql -h110.110.110.110 -P3306 -uroot -p
mysql -uroot -p -S /data1/mysql5/data/mysql.sock -A --default-character-set=GBK
}
shell执行mysql命令{
mysql -u$username -p$passwd -h$dbhost -P$dbport -A -e "
use $dbname;
delete from data where date=('$date1');
" # 执行多条mysql命令
mysql -uroot -p -S mysql.sock -e "use db;alter table gift add column accountid int(11) NOT NULL;flush privileges;" # 不登陆mysql插入字段
}
备份数据库{
mysqldump -h host -u root -p --default-character-set=utf8 --hex-blob dbname >dbname_backup.sql # 不包括库名,还原需先创建库,在use
mysqldump -h host -u root -p --database --default-character-set=utf8 --hex-blob dbname >dbname_backup.sql # 包括库名,还原不需要创建库
/bin/mysqlhotcopy -u root -p # mysqlhotcopy只能备份MyISAM引擎
mysqldump -u root -p -S mysql.sock --default-character-set=utf8 --hex-blob db table1 table1 > /data/db.sql # 备份表
mysqldump -uroot -p123 -d database > database.sql # 备份数据库结构
innobackupex --user=root --password="" --defaults-file=/data/mysql5/data/my_3306.cnf --socket=/data/mysql5/data/mysql.sock --slave-info --stream=tar --tmpdir=/data/dbbackup/temp /data/dbbackup/ 2>/data/dbbackup/dbbackup.log | gzip 1>/data/dbbackup/db50.tar.gz
# xtrabackup备份需单独安装软件 优点: 速度快,压力小,可直接恢复主从复制
}
还原数据库{
mysql -h host -u root -p dbname < dbname_backup.sql
source 路径.sql # 登陆mysql后还原sql文件
}
赋权限{
# 指定IP: $IP 本机: localhost 所有IP地址: % # 通常指定多条
grant all on zabbix.* to user@"$IP"; # 对现有账号赋予权限
grant select on database.* to user@"%" Identified by "passwd"; # 赋予查询权限(没有用户,直接创建)
grant all privileges on database.* to user@"$IP" identified by 'passwd'; # 赋予指定IP指定用户所有权限(不允许对当前库给其他用户赋权限)
grant all privileges on database.* to user@"localhost" identified by 'passwd' with grant option; # 赋予本机指定用户所有权限(允许对当前库给其他用户赋权限)
grant select, insert, update, delete on database.* to user@'ip'identified by "passwd"; # 开放管理操作指令
revoke all on *.* from user@localhost; # 回收权限
}
更改密码{
update user set password=password('passwd') where user='root';
flush privileges;
mysqladmin -u root password 'xuesong'
}
mysql忘记密码后重置{
cd /data/mysql5
/data/mysql5/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
update user set password=password('123123') where user='root';
}
mysql主从复制失败恢复{
slave stop;
reset slave;
change master to master_host='10.11.241.126',master_port=3306,master_user='root',master_password='passwd',master_log_file='master-bin.00009,master_log_pos=98,master_connect_retry=60';
slave start;
}
}
转载于:https://blog.51cto.com/2364821/1260747