Debian install Cassandra + example db

These commands will add the repo / keys and install cassandra v2

echo -e "\ndeb 20x main" >> /etc/apt/sources.list
echo -e "\ndeb-src 20x main" >> /etc/apt/sources.list
gpg --keyserver --recv-keys F758CE318D77295D
gpg --export --armor F758CE318D77295D | apt-key add -
gpg --keyserver --recv-keys 2B5C1B00
gpg --export --armor 2B5C1B00 | apt-key add -

apt-get update
apt-get install cassandra

Set up a test db

CREATE KEYSPACE mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};
USE mykeyspace;
  user_id int PRIMARY KEY,
  fname text,
  lname text
INSERT INTO users (user_id,  fname, lname)
  VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id,  fname, lname)
  VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id,  fname, lname)
  VALUES (1746, 'john', 'smith');

You may also want to install sun java

echo "deb precise main" | tee -a /etc/apt/sources.list
echo "deb-src precise main" | tee -a /etc/apt/sources.list
apt-key adv --keyserver --recv-keys EEA14886
apt-get update
apt-get install -y --force-yes oracle-java7-installer

Afterwards, don’t forget

apt-get install -y --force-yes oracle-java7-set-default

[FIXED] Telerik SqLite Unable to find the requested .Net Framework Data Provider. It may not be installed.

Add to App.config :

     <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data   Provider for SQLite"
      type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />

Here’s my full config:

<?xml version="1.0" encoding="utf-8"?>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    <requiredRuntime version="v4.0.20506" />
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data   Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    <add name="Connection1" connectionString="data source=database.db;version=3;useutf8encoding=True" providerName="System.Data.SQLite" />

Other things to check:

-That you are actually referencing the required libraries
-That said libraries are set to “Copy Local” (defaults to False for me!)

Switchable content

Code Behind

Extend the ContentControl class to add a template selector to it

public class ContentControlEx : ContentControl
	public DataTemplateSelector TemplateSelector
		get { return (DataTemplateSelector)GetValue(TemplateSelectorProperty); }
		set { SetValue(TemplateSelectorProperty, value); }

	public static readonly DependencyProperty TemplateSelectorProperty =
		DependencyProperty.Register("TemplateSelector", typeof(DataTemplateSelector), typeof(ContentControlEx), new PropertyMetadata(null));

	protected override void OnContentChanged(object oldContent, object newContent)
		base.OnContentChanged(oldContent, newContent);
		if (this.TemplateSelector != null)
			this.ContentTemplate = this.TemplateSelector.SelectTemplate(newContent, this);


-Resource imports the templates/styles file (see below).
-In this example content in column 1 is switchable via ContentControlEx (see above) based on what item is selected in xyz (in column 0, which is a listbox/treeview etc)

	<ResourceDictionary Source="DetailsTemplates.xaml"/>

<ListBox x:Name="xyz" Grid.Column="0" Padding="5" BorderThickness="0"
	ItemTemplate="{StaticResource EmployeeTemplate}"
	ItemsSource="{Binding Employees}"

<local:ContentControlEx Grid.Column="1"
	TemplateSelector="{StaticResource DetailsTemplateSelector}"
	DataContext="{Binding Path=SelectedItem, ElementName=xyz}" Content="{Binding}" />

As you can see, content is bound to {Binding}, this is because column 0 and 1 both use the same data (an Employee) so we can simply bind to the SelectedItem in the listbox or w/e you are using. Column 0 only shows his/her name and on selection, column 1 will show a whole lot more.


-Provides the DetailsTemplateSelector used by ContentControlEx above
-Holds styles and DataTemplates we want to use

<local:MasterDetailTemplateSelector x:Key="DetailsTemplateSelector"
	EmployeeTemplate="{StaticResource EmployeeDetailsTemplate}"
	OfficeTemplate="{StaticResource OfficeDetailsTemplate}"/>

<Style TargetType="TextBlock" x:Key="myKey">
<DataTemplate x:Key="EmployeeTemplate">
	<StackPanel Orientation="Horizontal">
		<TextBlock Text="{Binding FirstName}" Margin="5,0" />
		<TextBlock Text="{Binding LastName}" />
<DataTemplate x:Key="EmployeeDetailsTemplate">
		<local:UCDataTempl />
<DataTemplate x:Key="OfficeDetailsTemplate">

-So in here we got our styles (cut out), the DataTemplate for column 0 (the employee selector) and then the DateTemplate for column 1 (the detailed view for selected employee.)

-More templates can be put in here, like an OfficeDetailsTemplate in case you’d want to use a treeview where employees are child nodes of offices. The template selector will select the appropriate template based on the SelectedItem’s type.

-I set the details template to a user control so I can graphically design it. Within this control I can still access Employee data by simply using “{Binding Name}” etc. without additional configuration.

Code Behind (Seperate file called MasterDetailTemplateSelector.cs)

-Switch to the appropriate data template based on what was selected
-This is automatically called by ContentControlEx when it’s content changes (so when the selected item in “xyz” changes because that’s what it’s bound to.)

public class MasterDetailTemplateSelector : DataTemplateSelector
	public DataTemplate OfficeTemplate { get; set; }
	public DataTemplate EmployeeTemplate { get; set; }

	public override DataTemplate SelectTemplate(object item, DependencyObject container)
		Office company = item as Office;
		if (company != null)
			return this.OfficeTemplate;

		Employee employee = item as Employee;
		if (employee != null)
			return this.EmployeeTemplate;

		return base.SelectTemplate(item, container);

Telerik color set and use


<telerik:Windows8Colors x:Key="Windows8Colors" />
<SolidColorBrush x:Key="AccentBrush" Color="{Binding Source={StaticResource Windows8Colors}, Path=Palette.AccentColor}" />


void x()
    Windows8Colors.PaletteInstance.AccentColor = Color.FromArgb(0xFF, 0x79, 0x25, 0x6B);


    <TextBlock FontSize="14" Foreground="{StaticResource AccentBrush}" />

Bash script for adding a mysql instance

Because I wanted to use one server to replicate multiple mysql masters I had to create various mysql instances from the same mysql install, which got tedious, so I made a script. Just enter the idbase (that with the port appended will become your sever id for replication), the port and the password you want for root.

It will first create the data directory and config file. After that it starts the server, sets the root password and then it adds the server info to the phpmyadmin file so you can select it when logging in.

idbase=132 #Because the local address of this example server is, easy to identify
port=3307 #Up by 1 for each instance

#Create the data directory and log directory (if you uncomment it)
mkdir /var/lib/mysql$port
chown -R mysql.mysql /var/lib/mysql$port/
#Uncomment these if you want to enable logging
#mkdir /var/log/mysql$port
#chown -R mysql.mysql /var/log/mysql$port
cp /etc/mysql/my.cnf /etc/mysql/my$port.cnf

#Configure the server. Tested on mysql version 5.5.28
sed -i "s/#server-id.*=.*/server-id = $idbase$port/" $file
sed -i "s/server-id.*=.*/server-id = $idbase$port/" $file
sed -i "s#^\(port\s*=\s*\).*\$#\1$port#" $file
sed -i "s#^\(socket\s*=\s*\).*\$#\1/var/run/mysqld/mysqld$port.sock#" $file
sed -i "s#^\(pid-file\s*=\s*\).*\$#\1/var/run/mysqld/mysqld$" $file
sed -i "s#^\(datadir\s*=\s*\).*\$#\1/var/lib/mysql$port#" $file
sed -i '/^bind-address/s/bind-address.*=.*/#bind-address =' $file

#Install the db, start it, then set root password
mysql_install_db --user=mysql --datadir=/var/lib/mysql$port/
nohup mysqld_safe --defaults-file=/etc/mysql/my$port.cnf &
sleep 3 #there's no telling if this waits long enough for the instance to start, so make sure the next command executed succesfully
mysqladmin -S /var/run/mysqld/mysqld$port.sock -u root password $rootpass

#Add the new server to phpmyadmin
echo "\$i++;
\$cfg['Servers'][\$i]['verbose'] = 'localhost $port';
\$cfg['Servers'][\$i]['host'] = '';
\$cfg['Servers'][\$i]['port'] = '$port';
\$cfg['Servers'][\$i]['socket'] = '';
\$cfg['Servers'][\$i]['connect_type'] = 'tcp';
\$cfg['Servers'][\$i]['extension'] = 'mysqli';
\$cfg['Servers'][\$i]['auth_type'] = 'cookie';" >> /etc/phpmyadmin/

Don’t forget to add the startup command for each instance to /etc/rc.local if you want it to start at boot:

nohup mysqld_safe --defaults-file=/etc/mysql/my3307.cnf &

To reboot:

mysqladmin -S /var/run/mysqld/mysqld3307.sock --password=xx shutdown
nohup mysqld_safe --defaults-file=/etc/mysql/my3307.cnf >> /dev/null 2>&1 &

Options altered by script:

#edit /etc/mysql/my$port.cnf
port = $port
socket = /var/run/mysqld/mysqld$port.sock

socket = /var/run/mysqld/mysqld$port.sock

pid-file = /var/run/mysqld/mysqld$
socket = /var/run/mysqld/mysqld$port.sock
port = $port
datadir = /var/lib/mysql$port
#bind-address = #comment it out
server-id = $idbase$port

I also made this script to quickly configure the slave. This assumes you used the first script because it uses the same file structure (/etc/mysql/my$slave_port.cnf etc.).

#User config

#Vars for ease of use
dbwilddo_done="replicate-wild-do-table = $dbwilddo"

#Read line 85 to see if config file is correctly positioned
readline=$(sed -n '85p' $conf_file)
#Read line 86 to see if config file is already altered
readline2=$(sed -n '86p' $conf_file)

if  [[ $readline == server-id* ]] ;
	if  [[ $readline2 == "$dbwilddo_done" ]] ;
		echo lines already added, skipping
		echo Updating mysql config file
		#Write the name of the table we want to replicate
		sed -i "86i$dbwilddo_done" $conf_file
		#--DISABLED--Write connection info--DISABLED--
		#write_one="master-host = $master_host"
		#sed -i "87i$write_one" $conf_file
		#write_one="master-port = $master_port"
		#sed -i "88i$write_one" $conf_file
		#write_one="master-user = $master_user"
		#sed -i "89i$write_one" $conf_file
		#write_one="master-password = $master_password"
		#sed -i "90i$write_one" $conf_file
	echo config file line 85 did not return the expected value

echo "Restarting mysql"
mysqladmin -S /var/run/mysqld/mysqld$slave_port.sock --password=$slave_pass shutdown
nohup mysqld_safe --defaults-file=/etc/mysql/my$slave_port.cnf >> /dev/null 2>&1 &
sleep 5 #Hopefully long enough
#mysqld_safe --defaults-file=/etc/mysql/my$slave_port.cnf restart #Doesn't work, still looking for a simpler method

#Create database
echo "Creating database if it doesn't already exist"
mysql --host= --port=$slave_port --password=$slave_pass -e "CREATE DATABASE IF NOT EXISTS $database"

#Set master
echo "Stopping slave"
mysql --host= --port=$slave_port --password=$slave_pass -e "STOP SLAVE;"
echo "Setting master"
mysql --host= --port=$slave_port --password=$slave_pass -e "CHANGE MASTER TO MASTER_HOST='$master_host',MASTER_USER='$master_user',MASTER_PASSWORD='$master_password',MASTER_PORT=$master_port;"

#Do manually:
#Import database(faster than just syncing, if db is big it'll save alot of time)
	#Master: mysqldump --password=xx dbname | gzip > /var/file.sql.gz
	#Transfer it
	#Slave: gunzip file.sql.gz
	#Slave: mysql --host= -P 3307 --password=xx dbname < /var/file.sql.gz
#Enable replication (START SLAVE;) + fix any errors (usually duplicate keys since sync/import).
#Sync with database (install pt-table-sync below) to make sure we missed nothing since the import
	#pt-table-sync --execute --databases dbname h=master,u=muser,p=mpass h=,u=root,p=xx
#Alternatively lock the write table prior to dump+import and then release lock when done. I don't do this because above method avoids downtime.
#Install pt-table-sync if you need it. This code is for Debian distros only!
	#cd /var
	#dpkg -i percona-toolkit_2.1.7_all.deb
	#apt-get -f install
	#dpkg -i percona-toolkit_2.1.7_all.deb

Finding missing IDs

Stuff happens during initial replication, and you could end up missing a couple of IDs. For some reason pt-table-sync doesn't always pick them up for me so I made a linux shell command that detects the gaps, but you'll have to export all the IDs to a text file first, in reverse order:

nohup mysql --password=xx -e 'select id from tablename order by id desc' databasename > /home/ids.txt &

Then parse the file with the shell command. The first line and last two lines are just to keep track of how long it took. 1.5million IDs(ish) took me 57sec & that's on a slow server. Set the max id in i and have at it.

T="$(date +%s)"; \
i=1574115; \
while read line; do \
	if  [[ "$line" != "$i" ]] ; then \
		if [[ $i -lt 1 ]] ; then break; fi; \
		if  [[ $line -gt 1 ]] ; then \
			missingsequenceend=$(( $line + 1 )); \
			minusstr="-"; \
			missingsequence="$missingsequenceend$minusstr$i"; \
			expectnext=$(( $line - 1 )); \
			i=$expectnext; \
			echo -e "$missingsequence"; \
		fi; \
	else \
		i=$(( $i - 1 )); \
	fi; \
done \
< ids.txt; \
T="$(($(date +%s)-T))"; \
echo "Time in seconds: ${T}"