Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

My SQL 101


Published on

MySQL basics

Published in: Technology
  • Be the first to comment

My SQL 101

  1. 1. MySQL 101 MySQL (play /maɪ ˌɛskjuːˈɛl/ "My S-Q-L",[3] officially, but also incorrectly called /ma ɪ ˈsiːkwəl/ "My Sequel") is the worlds most used[4] relational database management system (RDBMS)[5] that runs as a server providing multi-user access to a number of databases. The SQL phrase stands for Structured Query Language.[7] – Wikipedia 1 Http://
  2. 2. Agenda➔ Installation➔ Starting MySQL➔ Stopping MySQL➔ Connecting to MySQL➔ Loading data➔ Looking at data➔ Backup➔ Login/Authentication➔ Where to go from here➔ Suggestions➔ Questions and @stoker 2
  3. 3. Installation MySQL is available for most Operating Systems Binaries, RPMS and DEBs available Windows Source Code Is what I recommend 3
  4. 4. Packages  sudo apt-get install mysql5  rpm -Uhv mysql5 May have slightly different names, may have client utilities separate 4
  5. 5. Binariesshell> groupadd mysqlshell> useradd -r -g mysql mysqlshell> cd /usr/localshell> tar zxvf /path/to/mysql-VERSION-OS.tar.gzshell> ln -s full-path-to-mysql-VERSION-OS mysqlshell> cd mysqlshell> chown -R mysql .shell> chgrp -R mysql .shell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql data# Next command is optionalshell> cp support-files/my-medium.cnf /etc/my.cnfshell> bin/mysqld_safe --user=mysql &# Next command is optionalshell> cp support-files/mysql.server /etc/init.d/mysql.server 5
  6. 6. Configuration Files shell> cp support-files/my-medium.cnf /etc/my.cnf  The configuration files have not aged well. They were written long ago. Please read through the choices to find one that best matches your system.  MySQL will use built-it defaults, almost guaranteed not to be optimal for your environment if no my.cnf is present 6
  7. 7. Starting MySQLshell> bin/mysqld_safe --user=mysql & This command is what starts your MySQL server. It runs awrapper script as user mysql in the background. You might alsosee this wrapper wrapped/etc/init.d/mysql start or service mysql start 7
  8. 8. mysqld What is that wrapper running? The mysqld binary /usr/local/mysql/bin/mysqld –user=mysql (And other options from /etc/mysql/my.cnf) 8
  9. 9. /etc/mysql/my.cnf Options for your [client] port=3306 MySQL Server [mysql] default-character-set=latin1 Please use as CLI [mysqld] # The TCP/IP Port the MySQL Server will listen on options are not good port=3306 for long term sanity! #Path to installation directory. All paths are usually resolved relative to this. basedir="C:/Program Files/MySQL/MySQL Server 5.5/" Use your favorite #Path to the database root change control datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.5/Data/" program to track # The default character set that will be used when a new schema or table is changes # created and no character set is defined character-set-server=latin1 # The default storage engine that will be used when create new tables when default-storage-engine=INNODB 9
  10. 10. Configuration file has sections for the various MySQL Programs[client] Settings for client programsport=3306[mysql] General settingsdefault-character-set=latin1[mysqld] Server settings# The TCP/IP Port the MySQL Server will listen onport=3306#Path to installation directory. All paths are usually resolvedrelative to this.basedir="C:/Program Files/MySQL/MySQL Server 5.5/" 10
  11. 11. STOP!!!!!!  service mysql stop  /etc/init.d/mysql stop mysqladmin -u root shutdown 11
  12. 12. Connecting shell> mysql db_name shell> mysql --user=user_name --password=your_password db_name mysql db_name < script.sql > output.tabUse the mysql client program to connect to the server 12
  13. 13. Connect to Another Host shell> mysql –host=host db_name shell> mysql -h host db_name These two commands are equivalent 13
  14. 14. Connected 14
  15. 15. s (stats) 15
  16. 16. SHOW DATABASES terminate with ; or g ; g 16
  17. 17. q Use q to exit the mysql client 17
  18. 18. Loading Data  Example databases  World  Sakila  Download from shell> mysql mysql> create database worldg mysql> use worldg mysql> source world_innodb.sql 18
  19. 19. Looking At Data mysql> show tables; mysql> select * from City; mysql> SELECT Name, CountryCode --> FROM City → WHERE Population > 10000000; 19
  20. 20. Backup  Physical versus logical  Many options, many tools  Restoration from backup needs to be tested  shell> mysqldump --all-databases > dump.sql  shell> mysqldump --databases db1 db2 db3 > dump.sql (and to restore)  shell> mysql < dump.sql 20
  21. 21. Login/Authentication  MySQL authentication is a little primitive  The mysql database has tables for login information  Easy to get confused  Use a tool like MySQL Workbench  Be stingy with permissions  Read chapter 6 of the MySQL manual 21
  22. 22. mysql.user table  Host  User  Password  Privileges  Connection constraints (ssl, time, # connections)  The server will first check Host address first, then user and password.  Joe @ can have separate privs than Joe @ and % @ can trump Joe @ foo.net 22
  23. 23. Workbench Administrative View 23
  24. 24. You can use the CLI but ... Yes, you can type in themysql> select * from user where User=joe limit 1G*************************** 1. row *************************** Host: % User: joe settings for a new account Password: Select_priv: N Insert_priv: N Update_priv: N by hand but it is easy to Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N fat finger one of the thirty Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N privs. References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Other tools have similar Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N features and you should Trigger_priv: NCreate_tablespace_priv: N ssl_type: ssl_cipher: use them to avoid dumb x509_issuer: x509_subject: max_questions: 0 max_updates: 0 errors. max_connections: 0 max_user_connections: 0 plugin: authentication_string: NULL 24
  25. 25. Where To Go From Here Training  Classes   Local user groups or colleges  Webinars  Conferences  MySQL Connect/Oracle Open World  MySQL Innovation Day (Webcast)  SELF  Planet.MySQL.Com  Forums.MySQl.Com 25
  26. 26. Suggestions MySQL Administrators Bible – Sheeri Cabral 26
  27. 27. Also SuggestedHigh Performance MySQL- Schwartz, Zaitsev, andTkachenko3rd Edition!!! 27
  28. 28. A New SeriesEffective MySQL Backup and Recovery, EffectiveMySQL Optimizing Statements, Ronald Bradford 28
  29. 29. MySQL CertificationAging but stillthe CertificationGuide 29
  30. 30. Questions/ @stoker 30