INSERT INTO table (ip) VALUES (INET_ATON('$ip_address'));
And done.
/bb|[^b]{2}/
Never stop Grokking
Monday, November 23, 2009
Storing IP addresses in a MySQL data table
For a lot of log processing, I need to store IP addresses in a database table. The standard process was always to convert it to an unsigned int in perl or php and then insert it. Today I discovered an easier way. MySQL's INET_ATON function. It takes an address in dotted quad format and converts it into an INT. So, all you have to do is this:
Labels
4.01-strict
404
accessibility
acer
airport wifi
apache
australia
ayttm
badges
bandwidth
bbc
bcp
berlin
blog
blogger
blogger template
bof
book
boomerang
broken
bug
byte order
c
closure
cmc
cms
codepo8
colours
comic strip
comments
communication
compile
conference
confoo
cracker
crash
creative
crockford
cron
css
data tags
database
date
db
delicious
dhtml
dom
dopplr
dragdrop
dynamic script node
education
email
endianness
epicondylitis
error checking
esmtp
everybuddy
extensions
favicon
fc9
fedora
filesystem
firefox
flickr
flot
fosdem
foss
foss.in
freebsd
freedom
function currying
gdb
geek
geo
gmail
gnome
hack
hacker
hardy
hash
howtos
html
http
i18n
iit
im
instant messaging
internet
ip
ip address
ipc
iso8601
jabber
javascript
json
keynote
latency
latex
LC_TIME
linux
localisation
login
lsm
macosx
mail
mathematics
mathjax
measurement
meetup
memory
microformats
missing kids
montreal
movable type
mvc
mysql
name generator
network
notes
opensource
partition
performance
perl
php
planet
ports
programming
programming style
progressive enhancement
recovery
redhat
regex
regular expressions
rfc2822
rfc3339
rhel
roundtrip
rss
safari
sampling
scalability
scripting
secnet
security
sed
segfault
self extracting tarball
sendmail
server
shell
shell script
sigdashes
site
slideshare
smtp
sockets
ssl
starttls
statistics
stoyan
strftime
stubbornella
sydney
sysadmin
tablespace
talks
theme
thisisbroken
thisisfixed
thoughts
tim berners-lee
timezone
tips
toc
toy
transactions
twitter
typing
ubuntu
unix
url
velocity
vint cerf
w3c
web
webcam
webdev
webdu
webtiming
whois
wifi
workaround
write performance
X
yahoo
ydn
YQL
yslow
yui
Translate this page
Blog Archive
Twitter updates
- PHOTO FEED
- Blog feed
- © PHILIP TELLIS 2009
-

The other side of the moon by Philip Tellis is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
10 comments:
INET_ATON() is not IPv6 compatible, so it has little value.
well, mysql itself is not IPv6 compatible, so if you need IPv6, your best bet is to use a string, don't convert to superduperbigint or whatever datatype they invent to store it.
Just be careful not to use this function (INET_ATON) in a trigger. It seems a logical thing to want to do - create a before-insert trigger that takes a dotted-quad IP and converts it automatically to unsigned INT so that you don't have to mess with it - but, alas, it doesn't work. Returns null, if I recall correctly.
inet_aton and inet_ntoa are perfectly fine to use in triggers
http://pastebin.com/m6f1d8490
Saying ipv4 has little value does not match reality.
Many many many applications still assume ipv4; so this is still very relevant.
@Blues
I would think BINARY(16) is a much better choice than VARCHAR(39) for IPv6 addresses.
@gtowney: you're right, though you'd have to deal with the complexity of converting it to/from a number in your application in that case. Two bigints - one for the network and one for the host could also work (correct me if I'm wrong).
just perfect. this will come in handy someday and i am thinking very soon... thanx
I am also pretty sure that
MYSQL -->INET_ATON() is the same as PHP-->ip2long()
and
MYSQL-->INET_NTOA() is the same as PHP-->long2ip()
Here is more on IP address
Get Visitors IP address
Post a Comment