Press "Enter" to skip to content

Tag: MySQL

安装Pyhton MySQLdb

安装很简单,几步就好

wget -q http://peak.telecommunity.com/dist/ez_setup.py
python ./ez_setup.py

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar -zxvf MySQL-python-1.2.3.tar.gz 
cd MySQL-python-1.2.3
python setup.py install

可能出现问题1:error: command ‘gcc’ failed with exit status 1
解决:yum install python-devel

Leave a Comment

MySQL Usage

1. User Management

# Create a database (replace '<DB NAME>' to your Database name)
CREATE DATABASE <DB NAME> CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

# Create a user and grant the database's permission to it
grant all privileges on <DB NAME>.* to <USER_NAME>@localhost identified by '<PASSWORD>';
# In MySQL 8.0 and later, the use of the IDENTIFIED BY clause with GRANT is deprecated. 
# Instead, you should use CREATE USER and ALTER USER statements to create and modify users,
CREATE USER '<USER_NAME>'@'localhost' IDENTIFIED BY '<PASSWORD>';
GRANT ALL PRIVILEGES ON <DB NAME>.* TO '<USER_NAME>'@'localhost';
flush privileges;

# Create a user and grant parts of the database's permission
grant select,update on <DB NAME>.* to <USER_NAME>@localhost identified by '<PASSWORD>';
CREATE USER '<USER_NAME>'@'localhost' IDENTIFIED BY '<PASSWORD>';
GRANT select,update ON <DB NAME>.* TO '<USER_NAME>'@'localhost';
flush privileges;

# See the User's Permission
show grants for <USER_NAME>@localhost;

# Delete a database
drop database <DB NAME>;

# Delete a user
DELETE FROM mysql.user WHERE User="<USER_NAME>" and Host="localhost";
flush privileges;

# Change Password for a User
update mysql.user set password=password('<NEW_PASSWORD>') where User="<USER_NAME>" and Host="localhost";
flush privileges;

2. DATABASE and Table Management

# Check a DB's Charset and Collection
SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '<DB NAME>';


# Alter a DB's charset and Collection
ALTER DATABASE <DB NAME> CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;


# Check the DB size
SELECT table_schema "DB Name",
        ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM information_schema.tables GROUP BY table_schema; 


# Check the Table size (replace myDB with your DB name)
SELECT TABLE_NAME, table_rows, data_length, index_length, 
round(((data_length + index_length) / 1024 / 1024),2) "Size in MB"
FROM information_schema.TABLES WHERE table_schema = "<DB NAME>" ORDER BY (data_length + index_length) DESC;


# Optimize Table
OPTIMIZE TABLE <table name>;
OPTIMIZE TABLE <table 1>, <table 2>, <table 3>;

# Optimize Tables Using the Terminal
mysqlcheck -u root -p --auto-repair --optimize --myDB  # For a Single DB
mysqlcheck -u root -p --auto-repair --optimize --all-databases

3. Optimize Table

use <DB NAME>;

# Find tables need to be optimized
select table_name, data_length, data_free from information_schema.tables
where table_schema='<DB NAME>' order by data_free desc;
+-----------------------------------+-------------+-----------+
| table_name                        | data_length | data_free |
+-----------------------------------+-------------+-----------+
| wordpress_commentmeta             |       44028 |     19784 |
| wordpress_comments                |     2005588 |     13024 |
| wordpress_postmeta                |      569124 |       112 |
| wordpress_links                   |        1220 |         0 |
| wordpress_aioseo_posts            |       16384 |         0 |
| wordpress_terms                   |       38876 |         0 |
| wordpress_tla_rss_map             |           0 |         0 |
| wordpress_actionscheduler_groups  |       16384 |         0 |

the output shows some general information about the table. The following two numbers are important:

  • Data_length represents the amount of space the database takes up in total.
  • Data_free shows the allocated unused bytes within the database table. This information helps identify which tables need optimization and how much space will be released afterward.
optimize table wordpress_commentmeta, wordpress_comments;

# Check tables again
select table_name, data_length, data_free from information_schema.tables
where table_schema='<DB NAME>' order by data_free desc;
+-----------------------------------+-------------+-----------+
| table_name                        | data_length | data_free |
+-----------------------------------+-------------+-----------+
| wordpress_commentmeta             |       44028 |         0 |
| wordpress_comments                |     2005588 |         0 |
| wordpress_postmeta                |      569124 |       112 |
| wordpress_links                   |        1220 |         0 |
| wordpress_aioseo_posts            |       16384 |         0 |
| wordpress_terms                   |       38876 |         0 |
| wordpress_tla_rss_map             |           0 |         0 |
| wordpress_actionscheduler_groups  |       16384 |         0 |

The results of the optimization change the values of data_length and data_free of the optimized table. Both values are lowered, indicating:

1. The optimization freed the unused allocated memory.
2. The overall memory of the database is lower because of the released space.

3 Comments

清除mysql-bin.0000xx文件

最近查了两自己用的比较久的两个VPS,/usr/local/mysql/var下很多名为 mysql-bin.000001、mysql-bin.000002 之类的文件占用了很大的体积。而这个问题几乎在所有的VPS上都会遇到,只要使用的时间一久,MYSQL便会产生大量的日志文件塞满你的服务器。

这是数据库的操作日志,例如 UPDATE 一个表,或者 DELETE 一些数据,即使该语句没有匹配的数据,这个命令也会存储到日志文件中,还包括每个语句执行的时间,也会记录进去的。

这样做主要有以下两个目的:
1:数据恢复
如果你的数据库出问题了,而你之前有过备份,那么可以看日志文件,找出是哪个命令导致你的数据库出问题了,想办法挽回损失。
2:主从服务器之间同步数据
主服务器上所有的操作都在记录日志中,从服务器可以根据该日志来进行,以确保两个同步。

删除 mysql-bin.0000X 日志文件
mysql> reset master;
Query OK, 0 rows affected (8.51 sec)
然后再看看,这些体积大的mysql-bin.0000xx文件都不在了。

3 Comments

FreeRADIUS实现VPN流量控制功能

本文接上文《PPTP + FreeRADIUS + MySQL 安装与配置》,请先安装好PPTP + FreeRADIUS + MySQL,然后再继续本文的安装过程。

vi /usr/local/etc/raddb/radiusd.conf

# 取消注释712行, $INCLUDE sql/mysql/counter.conf

vi /usr/local/etc/raddb/sql/mysql/counter.conf

# 最底行加入如下代码:

sqlcounter monthlytrafficcounter {
    counter-name = Monthly-Traffic
    check-name = Max-Monthly-Traffic
    reply-name = Monthly-Traffic-Limit
    sqlmod-inst = sql
    key = User-Name
    reset = monthly
    query = "SELECT SUM(acctinputoctets + acctoutputoctets) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) > '%b'"
}
3 Comments

PPTP + FreeRADIUS + MySQL 安装与配置

FreeRADIUS 是实现 RADIUS 协议的开源软件,而 RADIUS 主要用来实现认证(Authentication)、授权(Authorization)以及计费(Accounting)功能。本文内容在Centos 5.7 32bit下测试成功。

一,VPN服务器安装配置

# 安装编译环境

yum install -y wget gcc gcc-c++ make

# 安装ppp

yum install -y ppp

# 安装PPTP VPN

wget http://hello-linux.googlecode.com/files/pptpd_with_freeradius_plugins.sh
chmod +x pptpd_with_freeradius_plugins.sh
./pptpd_with_freeradius_plugins.sh

注意:此PPTP VPN脚本已经加入了FreeRADIUS插件,不能脱离FreeRADIUS独立使用。如果你只想安装PPTP VPN的话,请不要使用此脚本。
此时如果提示“错误691:由于域上的用户名和/或密码无效而拒绝访问”,请不要担心,这是正常的。

13 Comments

Oracle 和 MySQL 中查询条件限制语句

在 MySQL 中查询:
select * from table_name limit 100,20; 从第100条开始,查询20条数据;
select * from table_name limit 20; 从第1条开始,查询20条数据;

在 Oracle 中查询:
select * from table_name where rownum<=10

1 Comment