Intro to MySQL MySQL (Windows) MySQL (Ubuntu Desktop) MySQL...
Transcript of Intro to MySQL MySQL (Windows) MySQL (Ubuntu Desktop) MySQL...
DBMS : 1: Intro to MySQL
2: MySQL (Windows)
3: MySQL (Ubuntu Desktop)
4: MySQL (CentOS Server)
5: Tuning MySQL (Linux)
DBMS
part 1/5: Intro to MySQL
1. Intro to
• MySQL is the world's most popular open source database
– used by high profile web properties including Facebook, Twitter, YouTube, Yahoo! and many more
– Oracle drives MySQL innovation, delivering new capabilities to power next generation web, cloud, mobile and embedded applications
Spring 3 Junqiang Jimmy Liu
1. Intro to
• https://www.mysql.com/
• A variety of Editions: MySQL Enterprise Edition MySQL Cluster CGE MySQL Community Edition
• https://www.oracle.com/
• Oracle Database (19c)
• Java
Spring 4 Junqiang Jimmy Liu
DBMS
part 2/5: MySQL (Windows)
2.1 MySQL
– MySQL in Windows (5.5.20)
• MySQL database server (engine)
• MySQL Client (command line)
• MySQL Workbench 5.2.44 (GUI)
Spring 6 Junqiang Jimmy Liu
2.2 Installing MySQL
in Windows
• mysql-5.5.20-win32.msi
– MySQL database server (engine)
– MySQL Client (command line)
Spring 7 Junqiang Jimmy Liu
mysql-5.5.20 : MySQL Server Instance Configuration
• Usage: Mutlifunctional, Transactional only, Non-transactional
• InnoDB Tablespace Settings: physical location
• Approximate number of concurrent connections: OLAP, OLTP, Manual Setting
• Enable TCP/IP Networking: Port 3306
• Enable Strict Mode
• Default Character Set: Standard, Multilingualism
• Install As Windows Service with Name: MySQL
• Security - New root password: 123456
Spring 8 Junqiang Jimmy Liu
MySQL Workbench 5.2.44
• mysql-workbench-gpl-5.2.44-win32.msi
– MySQL Workbench 5.2.44 (GUI)
• Download prerequisites (as prompt)
– MySQL Workbench requires to be installed
• the Visual C++ 2010 Redistributable Package
• The .Net Framework 4.0
Spring 9 Junqiang Jimmy Liu
2.3 Start MySQL engine
• In Windows
– As a windows service (automatically start yes/no)
– Or, MySQL Workbench
Spring 10 Junqiang Jimmy Liu
DBMS
part 3/5: MySQL (Ubuntu Desktop)
3.1 MySQL in Ubuntu Desktop
– in Ubuntu Desktop (5.1.66)
• MySQL database server (engine)
• MySQL Client (mysql -u root -p) (command line)
• MySQL Navigator
• MySQL Administrator
• MySQL Query Browser
Spring 12 Junqiang Jimmy Liu
3.2 Installing MySQL
Search and install by “Software Center”
in Ubuntu Desktop
• MySQL Sever
• MySQL Client
• MySQL Administrator
• MySQL Query Browser
• MySQL Navigator
Spring 13 Junqiang Jimmy Liu
3.3 starting MySQL
• By GUI: MySQL Administrator
Server Hostname, user, password
• Tuning
• by command line (see next section)
Spring 14 Junqiang Jimmy Liu
3.4 using MySQL
• By GUI: MySQL Query Browser
– Executing any SQL statement in the browser
– show databases;
– show tables;
• by command line (see next section)
Spring 15 Junqiang Jimmy Liu
DBMS
part 4/5: MySQL (CentOS Server)
4.1 Install MySQL in command line
Initial (default) installation by command line only
yum install mysql-server • mysql-5.1.73-3.e16_5.x86-64.rpm
• mysql-libs-5.1.73-3.e16_5.x86-64.rpm
• mysql-server-5.1.73-3.e16_5.x86-64.rpm
• openssl-1.0.1e-30.e16.contos.9.x86-64
• perl-DBD-MySQL-4.013.e16.x86-64.rpm
Spring Junqiang Jimmy Liu 17
Setting up after initial (default) installation
• service mysqld start
• mysql_secure_installation – Current password for root (blank at the first time)
– Set root password : Y
– New password: 123456
– Remove anonymous users? [Y/n] n
– Disallow root login remotely ? [Y/n] n
• service mysqld stop
Spring Junqiang Jimmy Liu 18
4.2 Start MySQL in command line
start the engine by command line only
• service mysqld start
• mysql –u root -p
• service mysqld stop
Spring Junqiang Jimmy Liu 19
Another command line only
• mysqld_safe --verbose
• mysqld_safe --bind-address=172.31.100.203
Messages:
mysqld_safe Logging to syslog.
mysqld_safe Starting mysqld daemon with dbs from /var/lib/mysql
Spring 20 Junqiang Jimmy Liu
mysqld_safe options
--verbose
--help
--bind-address=name IP address to bind to.
--master-password=name
--master-port=#
--port=#
my.cnf, $MYSQL_TCP_PORT, /etc/services, 3306(default)
Spring 21 Junqiang Jimmy Liu
4.3 Stop MySQL in command line
• service mysqld stop
• mysqladmin -u root -p -f --verbose shutdown
• mysqladmin --user=root shutdown --password=xxxxx
Spring 22 Junqiang Jimmy Liu
4.4 Manage and Use MySQL
to check if mysql is running:
• mysqladmin -u root -p status
show you can retrieve data from the server:
• mysqlshow -u root -p
• mysqlshow -u root -p dbname
Spring 23 Junqiang Jimmy Liu
• Help in a terminal
man mysql
mysql –help
• Get into MySQL:
mysql –u user -p –h hostname dbname
mysql --user=xxxx --password=xxxxxx -e “sqlstmt;”
• Get out MySQL:
quit or exit Spring 24 Junqiang Jimmy Liu
DBMS
part 5/5: Tuning MySQL (Linux)
Across machines
Spring 26 Junqiang Jimmy Liu
5.1 Host IP
• Error connecting to a server – com.mysql.jdbc.CommunicationsException:
Communications link failure due to underlying exception
– java.net.SocketException MESSAGE: java.net.ConnectException: Connection refused: connect
• Fix:
– specifiying the host IP of the server either in my.cnf
– Or, as an option when start the server
mysqld_safe --bind-address=172.31.100.203
Spring 27 Junqiang Jimmy Liu
5.2 Firewall vs. Port
Possible connection rejection due to firewall,
please allow access to the port (default 3306)
• CentOS - adding entries into iptables
– iptables -A INPUT -i eth0 -p tcp --dport 3306 …
– …
• Ubuntu
– sudo ufw allow 3306
Spring 28 Junqiang Jimmy Liu
Disable the whole firewall
• service iptables stop
make it permanent
• chkconfig iptables off
Spring Junqiang Jimmy Liu 29
5.3 Allow remote users
• grant all on *.* to ‘root’@’172.31.100.202’ identified by ‘123456’;
Recorded as a row in mysql.user so that a user
at 172.31.100.202 can access the server as root
or
When trying connection from 172.31.100.202, got:
• Host ... is not allowed to connect to this MySQL server
• Access denied for user ‘root’ @ ‘172.31.100.202’
Spring Junqiang Jimmy Liu 30
5.4 Globalization : Character Set
• Start up options
– --character-set-client-handshake
– --character-set-filesystem=name
– --character-set-server=charset_name
– --collation-server=collation_name
• To know options,
– show variables like 'ch…';
• Option file
Spring 31 Junqiang Jimmy Liu
Charset/collation at client side
• SET NAMES 'charset_name' [COLLATE’collaiton_name'];
• Good 4 client-side (either at a terminal or a GUI)
• SET NAMES 'utf8'; equivalent to
– SET character_set_client='utf8';
– SET character_set_results='utf8';
– SET character_set_connection='utf8';
Spring 32 Junqiang Jimmy Liu
Four levels of charset/collation (Server, Database, Table, Column)
• The simplest way is to specify the default charset (and collation) when creating a database, a table, a column instead of specifying system variables in the option file or the options of starting up the server.
• Examples Good 4 server-side (either at a terminal or a GUI)
– create table chch (chch varchar(20)) charset 'utf8';
– alter database FacilitiesOnLine charset 'utf8';
– alter database FacilitiesOnLine character set 'utf8';
Spring 33 Junqiang Jimmy Liu
5.5 related conf files
• /etc/my.cnf • /etc/mysql/my.cnf the server configuration file. • /usr/etc/my.cnf • ~/.my.cnf bind-address = 127.0.0.1 port = 3306 • /etc/mysql/debian.cnf
• /var/run/mysqld/mysqld.sock socket file !!! • /var/lib/mysql/ubuntu.pid pid file !!!
• /etc/init.d/mysql
• /etc/hosts
Spring 34 Junqiang Jimmy Liu
DB and DBMS
Uniform Java API Specific driver for individual DBMS
Client (command line)
GUI
Spring 35 Junqiang Jimmy Liu