Showing posts with label braindump. Show all posts
Showing posts with label braindump. Show all posts

Sunday, May 15, 2011

Setting up a GIT server on Ubuntu

I have all my code in a GIT repository on my home machine.
Everytime I wanted to deploy the code for my pet project site I used to fire up winscp and copy the code manually to the server. Well, as anyone would guess this was not only inefficient but error prone. Also reverting back to an old version just in case I ran into issues used to be a series of deletes & copies of directories.

GIT to rescue
The "divineness" (you kidding me ! this is not a word !!), anyways, let me start again. The "divineness" in me finally made me create a git server on my hosting server (slicehost). BTW, 2 words for my hosting company(its long due), I wanted to have a linux server with full root access and do whatever I wanted. Maximum flexibility and control. Plus I wanted 24x7 support. Slicehost has both, their customer service is awesome !! I always log into their chat and ask them stuff and they go above and beyond everytime.

K, no further digressions, lets get dirty

Setting up you git server on Ubuntu
The first thing you need to do is to install git on the Ubuntu server. That's a breeze:
apt-get install git-core

Now lets setup a directory where git server will run out of -
mkdir /opt/gitrepo
cd /opt/gitrepo

Initialize git :
git init --bare
The --bare flag tells git that this is a brand new directory where git will be setup

Pushing to this repository from your home machine.
For the purpose of this post, lets assume I have my git running at c:\myrepo also I'm writing this from a windows machine, so I'm in my gitbash

k, the first thing is telling your machine the server location.
git remote add origin ssh://planet@mydomainname.com/opt/gitrepo
Lets go over the command :
git remote add origin : I'm adding the origin of this repository as
ssh://planet@mydomainname.com/opt/gitrepo : 
ssh is the protocol
planet : this is the user name
@mydomainname.com : Well this is the name of your slice where you are setting the git server
/opt/gitrepo : The location of the server repo base we setup earlier

Push it to the server
Now lets push the master branch on your home machine to the server :
git push origin master
** master = This is the master branch on the home machine.
If you want to push some other branch, say "currentWorkingBranch", then type :
git push origin currentWorkingBranch

Once you run the command you'll see compression of files and pushing them off to the server, the output will end with something like :
* [new branch]      master -> master
OR
* [new branch]      currentWorkingBranch -> currentWorkingBranch

Verify on the server
On your server, go to /opt/gitrepo and type :
git branch
and you should see the branches you have pushed till now.

Remember, if you list the directories you WON'T see your code files.
Again, you WON'T see your code files.
Why ? - Because this is a server. You files are present internally and there is no working branch, hence you will get an error if you try this :
git status

Now you'll want to extract data out of this server - Read clone
So lets do that.
For testing create /temp/temprepo
in that just type :
git clone /opt/gitrepo/ /tmp/temprepo/
Now list the files and you'll see your code in /tmp/temprepo

btw, if you want to checkout a particular branch from the server, then first create a branch where you want to have the data :
e.g.
git branch branch-April2011 origin/branch-April2011
now checkout this branch :
git checkout branch-April2011
That's it you should have cloned the data only for that repository

The irc channel #git on freenode is very helpful, I'd suggest checking it out if you get stuck.
Ofcourse you can ask me about this.

Tuesday, April 27, 2010

Mysql Braindump = Tips & Tricks !

Note: These are braindumps !
While learning any topic I make notes, at first they are very basic and progressively they get advanced.
Don't expect super wisdom ("Gyan") out of these.
As I said just braindumps 8-)

If you have some tips you'd like to share, post in comments and I'll include them here for everybodys benefit.




MySQL
See all tables - "show tables;"
Configuring mysql for the first time after installation - The documentation from mysql is pretty good. I've just followed them, and have shown what the output of those commands are, just to satiate any paranoid developers like me 9-)

Remove unique constraint on MySql Table
This post describes how to do it, copied from there -
alter table TABLE_NAME drop index COLUMN_NAME;

Backup your MySQL database
mysqldump -u root -p DatabaseName > backuped_up_DB_DATE.sql
This will prompt you for your password.
The above is for the complete backup, including recreate statements & data.
Other variations are in the link.
Convert string to Date select column_name
from table
where date_column > str_to_date('31-jan-2010', "%d-%b-%Y")
and date_column< str_to_date('10-feb-2010', "%d-%b-%Y"); Drivers/connectors for Mysql for various platforms

Execute queries via mysql command prompt -
mysql db_name < text_file


Running mysql on Solaris for the first time
All commands are from the mysql installation directory.
default for Solaris is /opt/mysql
1.) Run the mysql_install_db command to create the basic grant tables
./mysql_install_db --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt//mysql/datadir --log-error=/opt//mysql/logs/mysql_install_db_log_file.txt
Output
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/mysql/mysql/bin/mysqladmin -u root password 'new-password'
/opt/mysql/mysql/bin/mysqladmin -u root -h password 'new-password'
Alternatively you can run:
/opt/mysql/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /opt/mysql/mysql ; /opt/mysql/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /opt/mysql/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /opt/mysql/mysql/scripts/mysqlbug script!
The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/
2.) Start the mysql database now
mysqld_safe --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt//mysql/datadir --log-error=/opt//mysql/logs/mysql_error_file.txt
Output on screen
100209 14:31:21 mysqld_safe Logging to '/opt//mysql/logs/mysql_error_file.txt'.
100209 14:31:21 mysqld_safe Starting mysqld daemon with databases from /opt//mysql/datadir
Output in the log file (log-error)
100209 14:31:21 mysqld_safe Starting mysqld daemon with databases from /opt//mysql/datadir
100209 14:31:21 [Note] Plugin 'FEDERATED' is disabled.
100209 14:31:21 InnoDB: Started; log sequence number 0 44233
100209 14:31:21 [Note] Event Scheduler: Loaded 0 events
100209 14:31:21 [Note] /opt/mysql/mysql/bin/mysqld: ready for connections.
Version: '5.1.42' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
3.) Shut it down from another prompt now
Shutdowm mysql deamon
mysqladmin -u root shutdown
Output in the log file (log-error)
100209 14:35:23 [Note] /opt/mysql/mysql/bin/mysqld: Normal shutdown
100209 14:35:23 [Note] Event Scheduler: Purging the queue. 0 events
100209 14:35:23 InnoDB: Starting shutdown...
100209 14:35:24 InnoDB: Shutdown completed; log sequence number 0 44233
100209 14:35:24 [Note] /opt/mysql/mysql/bin/mysqld: Shutdown complete
100209 14:35:24 mysqld_safe mysqld from pid file /opt//mysql/datadir/.pid ended
4.) Start again, this time with nohup so it can run in the background.
This is the actual command to start MySQL deamon
nohup mysqld_safe --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt//mysql/datadir --log-error=/opt//mysql/logs/mysql_error_file.txt &
Output in the log file (log-error)
100209 14:38:00 mysqld_safe Starting mysqld daemon with databases from /opt//mysql/datadir
100209 14:38:00 [Note] Plugin 'FEDERATED' is disabled.
100209 14:38:00 InnoDB: Started; log sequence number 0 44233
100209 14:38:00 [Note] Event Scheduler: Loaded 0 events
100209 14:38:00 [Note] /opt/mysql/mysql/bin/mysqld: ready for connections.
Version: '5.1.42' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
5.) Change the password of the mysql root user
/opt/mysql/mysql/bin/mysqladmin -u root password ''
6.) Secure the mysql database, by running this command and remove/disble all unsecure options
/opt/mysql/mysql/bin/mysql_secure_installation
The questions asked are -
Change the root password? [Y/n]
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n] Y <-- Select yes for this whether this is dev OR production
Remove test database and access to it? [Y/n]
Output you will see on the screen towards the end when everything needs to be saved
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
7.) Convert timezone in SQL query
Use this function, where "last_update_date" is the column name.
Below the timezone is being converted from UTC to EST.
convert_tz(last_update_date, '+00:00','-05:00')

8.) Sysdate equivalent in MySQL

Want to get records 5 days back ?
In oracle that sysdate-5 or sysdate-10 came so handy.
In MySQL, you can do date arithmetic by functions DATE_ADD & DATE_SUB.
Yes, as you would've guessed, DATE_SUB would subtract a certain amount.
Here is an example :

SELECT DATE_SUB(NOW(), INTERVAL 5 day);


The DATE_SUB takes the time you want to act on (NOW() gives the current date/time) and INTERVAL 5 day is giving the amount of time it wants to subtract from it.


http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

9.) Auto increment your integer column
When you want one of the columns to auto increment :

ALTER TABLE your_table

MODIFY some_column INT NOT NULL AUTO_INCREMENT;