Adding a user to MySQL


mysql.user table

MySQL uses a database called mysql to manage itself. Within this database is a table called 'user'. This holds references to all of the users and their permissions.

Example row from that table.
*************************** 1. row ***************************
Host: localhost
User: debian-sys-maint
Password: *F81097CE0391E26A0C9C87F7010DE5601DB99668
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: N
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
authentication_string: NULL

Create User:

MySQL provides a command called (you guessed it) CREATE USER. This will add a new user to the mysql.user table for you. If you wanted to you could also issue the respective INSERT, but who wants to do that!

Example of creating a new user
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

You will want to make sure you replace 'newuser' with the name of the user you wish to create. Also note, this user will have no permissions. In order for you to do anything worthwhile you will need to grant access. That is where the GRANT command and another post comes in handy.

Speed testing Javascript string methods


Today I was working on a project that required me to get the first character of a string. I started looking at the string methods and realized there is no lack of ways to do that, five to be exact (with native string methods). That made me thing about efficiency. I know that some methods are inherently faster than others but I have never taken that into consideration. I wanted to test which of these methods was the best choice for the action I needed.

I set about creating a test method that would run each of these options for a set number of iterations. Recording the start time and end time would allow me to determine the amount of time that had passed.

Here was my test bed:

I did not want to type each method in each time I wanted to test with a different number of iterations or a different option. Also, this would provide me with the ability to scale it later on if I wanted to include other methods. My findings were pretty interesting.

slice:     1554
split:     18203
substring: 1665
substr:    1842
charAt:    497

Think about this next time you need to iterate over some list of strings and you want to do it in the most efficient way possible.

Javascript single line conditional statements


Writing conditionals on a single line is something that is a bit of a controversy. On one side it leads to a very expressive format with the liberties it allows you to take advantage of. On the other side it could lead to code that is harder to read and follow than its less condensed counterpart.

I tend to be on the side of the human programmers who have to read the code. Not the human compilers who take one look at it and know exactly what it is doing. That may mean I an not as good of a developer, so be it. Code that is easier to read is easier to maintain, and extend. So even though you can do something like:

(apples) ? apples = 'red' : '';
if (apples) apples = (red);
apples && (apples = 'red');

I would personally prefer to do something like:

if (apples !== undefined) {
  apples = 'red';

Just like with any other coding practice it is best for you to determine what you like. What I like may be completely different than what you like. The number one rule to remember is when it comes to the guidelines of the project you are working on, what you like does not matter. You must follow the coding style already put in place. If you don’t your code will not merge well and could cause problems with maintenance later on.