ask the manowar fan
Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

So, you know I broke it again. But I guess I learnt some jazzy stuff about the configs. If you're using Rails and you keep getting these errors


Can't connect to local MySQL server through socket '/tmp/mysql.sock'(111)
Can't connect to local MySQL server through socket '/tmp/mysql.sock'(13)
Can't connect to local MySQL server through socket '/tmp/mysql.sock'(2)


You need to be editing your /etc/mysql/my.cnf as well as your /etc/mysql/debian.cnf and make them all point to either /tmp/mysql.sock or /var/run/mysqld/mysqld.sock whichever you feel works better with your Rails app.

A simple sudo /etc/init.d/mysql restart should work, if it doesn't you're a special special person with special special needs.

One more thing I've realized about myself, I can't drink coffee in the morning, very bad for me.

 

MySQL purge configuration

Posted In: , . By Sid

So MySQL is no fun, if you can't fool around with it. Fool around with it enough, and you'll have to just get rid of /etc/mysql/* and oddly enough apt-get doesn't replace those files if you do a clean install.

Ergo:


sudo apt-get remove --purge mysql-server
and
sudo apt-get install mysql-server


If you play with fire, you're going to get hurt. That simple man. Just use Boronyl or Soframycin!

In other news, on the topic of temperature, I'm so not used to this whole air conditioning thing man. Two t-shirts everyday and I'm still bloody freezing.

 

Playing around with MySQL UDFs

Posted In: , , , . By Sid

Okay, so I've been trying this since a long time and it wasn't working ... but I got some help and now it's working, thanks to Roland Bouman.

The end goal is as such:

Consider the tables inbox, ads, outbox. Everytime text is inserted into the inbox table a function should be called which analyses the inserted text and performs one of two operations:
1] Either the text should be inserted into the ads table, with correct specifications about what kind of ad it is.
2] The text should still be analysed and on the basis of that analysis a set of ads should be extracted from the ads table and this set should be pushed into the outbox table, to be sent out by an SMS gateway.


Now unfortunately for me, being the amateur that I am, I painted myself into a corner by selecting FLEX to generate the lexical analyser or text analyser. Never select technology without understanding the true meaning of flexibility. I would never have spent so much time against a wall, had I been able to re-write the (couple of thousand) lines of code the analyser is in, into MySQL code.

In technology terms I had to somehow ensure that a MySQL trigger would call the analyser code which would call further MySQL code.
1] MySQL Trigger - MySQL.
2] Analyser - C.
3] Further db processing - MySQL/C?

So the question arose how to go about accomplishing said goals.

MySQL UDF's:
These User-Defined-Functions allow you to install C functions as MySQL functions. It's really quite cool, kudos to the guys at MySQL for this beautiful extension mechanism.
MySQL UDF's.

MySQL C API:
Now the problem further arose, how could I pass on the results of the text analysis to MySQL code? The answer is obvious, write the MySQL db processing in the analyser itself! The UDF's allow you to return only pointers to strings, integers, doubles. No pointers to structures, unless I used some string typecasting...
MySQL C API

Problem that will "kill-you-till-you-die"
Now the problem that arose is that, everytime the MySQL C API would try to spawn a new connection it would return a "Cannot connect to server(111)" error.

I pegged it as "the client could not spawn a new connection from within the connection it was in".

According to the manual, the seventh parameter to the mysql_real_connect() unix_socket is not NULL, the string specifies the socket or named pipe that should be used. Ergo, it piggybacks the socket your already connected client uses instead of trying and failing to create a new one.

In my case, it was /tmp/mysql.socket

And now, problem solved.

A trigger on the inbox calls the function which analyses the text inserted into the inbox table and on the basis of that either inserts it into the ads table or retrieves a set of ads from the ads table and pushes an outgoing message into the outbox table.

VOILA!

"There's fighting on the left,
and marching on the right"
ACDC - The Razor's Edge

 


gcc -shared -o test_example.so `/usr/local/mysql/bin/mysql_config \
--cflags` test_example.c `/usr/local/mysql/bin/mysql_config --libs`


Assumes your filename is test_example.c and your output file will be test_example.so and you have MySQL installed in /usr/local/mysql/ with the --with-embedded-server option enabled when you configured the script.


"Murder, Mayhem"
Otep - Nein.

 

I would not recommend doing this. Use synaptic as far as possible, it's way cleaner and faster. In my case, I needed some additional configuration ergo I used source.

1. Step one download the source from the official MySQL downloads page. Pick the community server, and select the MySQL source tarball.

2. I referred to this wiki about how to install MySQL on Ubuntu from source most of the time, but somewhere down the line it failed me. Anyways, on with it. After downloading, extract it using:


tar xzvf mysql-yourversionnumber.tar.gz


3. Before you proceed you need to install three packages. build-essentials, libncurses5 libncurses5-dev. I used synaptic to do this.

4. Now, add a user and group allowing MySQL to run.

sudo groupadd mysql
sudo useradd -g mysql mysql


5. Now, change your directory to the one you just extracted into and use the following options for the configure script.


./configure \
--prefix=/usr/local/mysql \
--with-mysqld-user=mysql \
--without-debug \ (I didn't use this)
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \ (In case you want to enable dynamic loading use -rdynamic)
--disable-shared \ (I didn't use this)
--localstatedir=/usr/local/mysql/data \
--with-extra-charsets=none \ (I didn't use this)
--enable-assembler \ (I didn't use this)
--with-unix-socket-path=/tmp/mysql.socket


The options I used are as below, note, I used rdynamic for dynamic loading, so that I can call C functions within MySQL statements. I used the with-embedded-server option so that I can use the lmysqld library which compiles the MySQL C API which allows me to write MySQL code within a C program.


./configure \
--prefix=/usr/local/mysql \
--with-mysqld-user=mysql \
--with-client-ldflags=-rdynamic \
--with-mysqld-ldflags=-rdynamic \
--localstatedir=/usr/local/mysql/data \
--with-unix-socket-path=/tmp/mysql.socket \
--with-embedded-server


6. After this is done.


sudo make
sudo make install


The make took some time on my machine, at this point go read a book :)

7. Okay, now once this is done. What you need to do is setup the config file for MySQL giving it some default paramters for its connections.

sudo cp support-files/my-medium.cnf /etc/my.cnf


This is assuming you're still inside the MySQL-yourversionnumber directory you extracted the downloaded tarball in.

8. Now you've got to create the GRANT tables. This is easy just run a pre-provided script.

sudo /usr/local/mysql/bin/mysql_install_db --user=mysql


9. Now, you need to setup permissions. Here is where I messed up real bad, like took me two days to get it right after four OS re-installs bad.

So, the wiki says you need to do this.


sudo chown -R root /usr/local/mysql
sudo chown -R mysql /usr/local/mysql/var
sudo chgrp -R mysql /usr/local/mysql


Which looks all good, but my install didn't have a /usr/local/mysql/var directory and it erred here, which lead to another error in the next step. That is the step when you setup the MySQL daemon.

Instead, if the code above gives you an error. Go for this. Make sure you change to the directory you installed MySQL in, i.e. /usr/local/mysql/

chown -R root .
chown -R mysql data
chgrp -R mysql .


10. Once that's done. You need to start up the MySQL daemon (process which runs in the background)

/usr/local/mysql/bin/mysqld_safe -user=mysql&


If this doesn't say something bad like STOPPED then you're in the clear.

11. Set a password for the main user.

/usr/local/mysql/bin/mysqladmin -u root password 'new_password'


Login:

/usr/local/mysql/bin/mysql


12. Now, we setup the daemon to start itself automatically when your PC boots up.

sudo cp support-files/mysql.server /etc/init.d/mysql
sudo chmod +x /etc/init.d/mysql
sudo update-rc.d mysql defaults


Again, make sure you're still in the same directory that you extracted all the files in.

And you're done!

"Life goes on, and it's infuriating"
Lordi - It Snows In Hell.