mysql实践总结
首先介绍mysql的安装和基本使用、进阶操作、讲解mysql的导入导出和自动备份,然后介绍安全模式修改密码和mysql的全文本搜索功能,最后记录了个人使用mysql中遇到的问题集,闲暇时我也会多看几次,巩固下基础吧。
基础使用
sudo apt-get install mysql-common mysql-server
简单使用:建库
CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
建表
create table MyClass(id int() not null primary key auto_increment,name char() not null,sex int() not null default '',degree double(,));
增加
insert into MyClass values(,'Tom',96.45),(,'Joan',82.99), (,'Wang', 96.59);
删除
delete from MyClass where id=;
修改
update MyClass set name='Mary' where id=;
查询
select * from MyClass;
显示所有视图
select * from information_schema.TABLES where table_type='view' AND table_schema = '数据库名';
创建用户
create user xxx identified by 'password';
重命名
rename user aaa to bbb;
删除用户
drop user aaa;
显示权限
show grants for aaa(用户);
授予权限
grant select on xxx(数据库).* to aaa(用户);
授予某个数据库的全部权限
grant all on xxx(数据库).* to aaa(用户);
grant all on xxx(数据库).* to aaa(用户)@localhost;
取消授权
revoke all on *.* from aaa(用户)@localhost;
修改权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%’ WITH GRANT OPTION;
以上操作完成之后记得刷新权限:
flush privileges;
导入导出
导出数据和表结构:
mysqldump -uroot -p abc(数据库名) > abc.sql
敲回车后输入密码
只导出表结构
mysqldump -uroot -p -d abc > abc.sql
导入数据库
1、首先建空数据库
mysql> create database abc;
2、导入数据库
mysql -u root -p abc(数据库名) < abc.sql
数据库自动备份
新建备份脚本xxx.sh,输入以下内容
#!/bin/bash # 要备份的数据库名,多个数据库用空格分开
databases=("db1", "db2") # 备份文件要保存的目录,注意当前用户必须用户保存目录的读写权限
basepath='/root/backup/mysql/' if [ ! -d "$basepath" ]; then
mkdir -p "$basepath"
fi # 循环databases数组
for db in ${databases[*]}
do
# 备份数据库生成SQL文件
nice -n /usr/bin/mysqldump -uroot -pcd32d5e86e --database $db > $basepath$db-$(date +%Y%m%d).sql # 将生成的SQL文件压缩
nice -n tar zPcf $basepath$db-$(date +%Y%m%d).sql.tar.gz -C $basepath $db-$(date +%Y%m%d).sql # 删除7天之前的备份数据
find $basepath -mtime + -name "*.sql.tar.gz" -exec rm -rf {} \;
done # 删除生成的SQL文件
rm -rf $basepath/*.sql
使用crontab设置定时任务,在终端输入crontab -e,加入以下内容,此任务为每天3点自动执行。
* * * bash xxx.sh(此处填写脚本绝对地址)
开启日志记录
[mysqld]
server-id =
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1000M
binlog-format = row
安全模式操作
进入安全模式修改密码
mysqld_safe --skip-grant-tables & select user,host,password from user where user="root"
不同版本的mysql修改用户密码方式不一样,需要查看mysql->user中的密码字段,如果不是password的话就是authentication_string。
authentication_string的修改方式不太一样:
use mysql;
update user set authentication_string=PASSWORD("") where User='root';
update user set plugin="mysql_native_password";
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
如果不是authentication_string,则可用以下方法。
update user set password=PASSWORD("your_password") where user="root" and host=“localhost"
新操作
Mysql全文本搜索
Mysql5.6之后支持InnoDB,中文的全文本搜索,内置使用n-gram为分词处理器,还支持中文~。
创建索引
create fulltext index ngram_idx on tag(Title) with parser ngram;
或
alter table tag add fulltext index ngram_idx(Title) with parser ngram;
获取支持的最小分词长度
SHOW VARIABLES LIKE 'ft_min_word_len';
//unix系统可在/etc/my.cnf中修改
[mysqld]
ft_min_word_len =
开始使用
select Title,match(Title) against('清水') from tag ;
可能出现的问题集:
- 描述
--04T01::.004560Z mysqld_safe Logging to '/var/log/mysql/error.log'.
--04T01::.023009Z mysqld_safe A mysqld process already exists
解决方法:
$ sudo killall mysqld
- 描述
--04T01::.486677Z mysqld_safe Logging to '/var/log/mysql/error.log'.
--04T01::.488204Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
解决方法:
sudo mkdir -p /var/run/mysqld
sudo chown -R mysql:mysql /var/run/mysqld
- 描述
$ sudo /etc/init.d/mysql start
ies: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[....] Starting mysql (via systemctl): mysql.servicejob-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
解决方法:
当前文件夹不是实际目录导致
cd到一个实际目录位置即可
- 描述
sudo /etc/init.d/mysql start
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[....] Starting mysql (via systemctl): mysql.servicejob-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
按照提示:See "systemctl status mysql.service" and "journalctl -xe" for details.
但是并么有什么卵用,直接看mysql的log:/var/log/mysql/error.log
--04T01::.583745Z [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
解决方法:
杀掉所有mysqld进程:killall mysqld
再次sudo /etc/init.d/mysql start 成功
- 描述
dpkg被锁定
解决方法
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
mysql实践总结的更多相关文章
- Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...
- [MySQL实践] 实践记录
[MySQL实践] 实践记录 版权2019.5.17更新 MySQL MySQL各版本区别 一.选择的版本 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持 ...
- SQL Server链接MySQL实践
最近在访问多数据库的时候进行了SQLServer链接MySQL数据的实践,现总结如下: 一. 安装mysql-connector-odbc驱动: 1. 在SQL Server服务器的机器上安装mys ...
- linux使用yum的方式安装mysql实践
1.先检测是否已安装mysql ps -ef|grep mysql root : pts/ :: /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mys ...
- 挤点时间写博客-php&MySQL实践
hi 晚上要吃火锅的嘛,挤点时间写点东西吧,别被老板发现哦 1.PHP与MySQL 五.文章发布系统之后台 5.2 创建配置文件和初始化文件 为了统一配置以及管理方便,还有就是减少代码的冗余. 分别为 ...
- Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践
我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的便利,是的,我也是这样认为的,I know your felling . 前期了解概念什么的确实 ...
- (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html 我知道大家这段时间看了 ...
- 使用Prometheus+Grafana监控MySQL实践
一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...
- MySQL 实践
一.下载MySQL 1.mysql-noinstall-5.1.73-win32.zip 2.mysql-connector-net-6.8.3.msi 二.安装MySQL 三.连接MySQL 1.A ...
随机推荐
- 如何监控redis的cpu使用率
redis默认是单线程运行的,为了充分利用机器的cpu,正常情况下一台服务器上会装多个实例.如果通过top命令监控机器的cpu的话,监控值很笼统,不能精确到单redis实例的cpu使用率监控.而且ce ...
- Django基础—1
一. Django的安装1. 查看已安装的Django的版本 进入到终端以及Python的交互模式 python3/ ipython32. 交互模式中输入import django ...
- Springmvc <mvc:cros>和<mvc:intercepters>同时使用时,跨域被拦截了
问题原因:cros也是使用拦截器实现的,并且拦截器配置最后一个处理,导致在跨域处理之前调用了业务拦截器 解决方案:推荐使用http://software.dzhuvinov.com/cors-filt ...
- WeexSDK源码分析(iOS)
0.从工作原理谈起 Weex 表面上是一个客户端技术,但实际上它串联起了从本地开发.云端部署到分发的整个链路.开发者首先可在本地像编写 web 页面一样编写一个 app 的界面,然后通过命令行工具将之 ...
- WPF PrismDialog PopupWindowAction使用MetroWindow
本示例必须在prism5.0版本以上 PopupWindowAction如何使用MetroWindow? public class Window1ViewModel:BindableBase,II ...
- SpringBean作用域
1.Bean作用域 spring中为bean定义了5种作用域,分别为singleton(单例).prototype(原型).request.session和global session.默认情况下为s ...
- 10.TreeSet、比较器
Comparable和Comparator Comparable 简介 Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序". 即 ...
- JavaScript控制页码的显示与隐藏
前端页面开发分页显示功能时,一般都要求使用自定义的页码样式,直接用网上分页插件就比较麻烦了,这里记录一下工作中总结的一个比较简单通用的控制页码显示与隐藏的的js代码. 首先是使用时需要自己根据自己具体 ...
- react在router中传递数据的2种方法
概述 不传递数据叫什么单页面应用,渲染模块还需要http请求算什么单页面应用. 本文总结了react-router4中使用BrowserRouter时传递数据的两种方法,供以后开发参考,相信对其他人也 ...
- 写你的shell,其实很简单[架构篇]
引语:我本人以前并没有写过shell脚本,也许是因为懒,也许是没有被逼到要去写shell的地步.但是,前段时间,工作需求,要求重新跑几个月的脚本,这些脚本是每天定时进行跑的,而且每天是好几个脚本一起关 ...