| 1 |
|
|---|
| 2 | The following instructions are for 64-bit Ubuntu.
|
|---|
| 3 |
|
|---|
| 4 | Prepared by Prof. Chen Li.
|
|---|
| 5 |
|
|---|
| 6 | - Install Java:
|
|---|
| 7 |
|
|---|
| 8 | https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04
|
|---|
| 9 |
|
|---|
| 10 | shell> sudo apt-get update
|
|---|
| 11 | shell> sudo apt-get install default-jre
|
|---|
| 12 | shell> sudo apt-get install default-jdk
|
|---|
| 13 | shell> javac -version
|
|---|
| 14 |
|
|---|
| 15 | - Install MySQL
|
|---|
| 16 |
|
|---|
| 17 | https://help.ubuntu.com/community/JDBCAndMySQL
|
|---|
| 18 |
|
|---|
| 19 | shell> sudo apt-get install mysql-server
|
|---|
| 20 | shell> sudo apt-get install mysql-client
|
|---|
| 21 | shell> sudo apt-get install libmysql-java
|
|---|
| 22 |
|
|---|
| 23 | shell> mysql -u root -p
|
|---|
| 24 |
|
|---|
| 25 | mysql> CREATE USER 'mytestuser'@'localhost' IDENTIFIED BY 'mypassword';
|
|---|
| 26 | mysql> GRANT ALL PRIVILEGES ON * . * TO 'mytestuser'@'localhost';
|
|---|
| 27 |
|
|---|
| 28 | mysql> create database moviedb;
|
|---|
| 29 | mysql> use moviedb;
|
|---|
| 30 |
|
|---|
| 31 | mysql> create table stars(
|
|---|
| 32 | id varchar(10) primary key,
|
|---|
| 33 | name varchar(100) not null,
|
|---|
| 34 | birthYear integer
|
|---|
| 35 | );
|
|---|
| 36 |
|
|---|
| 37 | mysql> INSERT INTO stars VALUES('755011', 'Arnold Schwarzeneggar', 1947);
|
|---|
| 38 |
|
|---|
| 39 | mysql> INSERT INTO stars VALUES('755017', 'Eddie Murphy', 1961);
|
|---|
| 40 |
|
|---|
| 41 | mysql> select * from stars;
|
|---|
| 42 | mysql> exit
|
|---|
| 43 |
|
|---|
| 44 | shell> mysql -u mytestuser -p
|
|---|
| 45 | mysql> use moviedb;
|
|---|
| 46 | mysql> select * from stars;
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | - Open a new shell:
|
|---|
| 50 |
|
|---|
| 51 | shell> CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar
|
|---|
| 52 | shell> export CLASSPATH
|
|---|
| 53 | shell> javac JDBC1.java
|
|---|
| 54 | shell> java JDBC1
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | Start MySQL
|
|---|
| 58 | shell> sudo /etc/init.d/mysql start
|
|---|
| 59 |
|
|---|
| 60 | Stop MySQL
|
|---|
| 61 | shell> sudo /etc/init.d/mysql stop
|
|---|
| 62 |
|
|---|
| 63 | Restart MySQL
|
|---|
| 64 | shell> sudo /etc/init.d/mysql restart
|
|---|
| 65 |
|
|---|
| 66 | - To allow remote access to your AWS EC2 instance, follow instructions at https://mariolurig.com/coding/connect-remotely-mysql-database-amazon-ec2-server/ . In particular:
|
|---|
| 67 |
|
|---|
| 68 | - Change the security group of your EC2 instance to add a rule to allow your local machine to access the MySQL instance (port 3306);
|
|---|
| 69 |
|
|---|
| 70 | - On the EC2 instance, create a user for remote access:
|
|---|
| 71 |
|
|---|
| 72 | shell> mysql -u root -p
|
|---|
| 73 | mysql> CREATE USER 'mytestuser'@'%' IDENTIFIED BY 'mypassword';
|
|---|
| 74 | mysql> GRANT ALL PRIVILEGES ON * . * TO 'mytestuser'@'%';
|
|---|
| 75 | mysql> flush privileges
|
|---|
| 76 |
|
|---|
| 77 | - On the EC2 instance, edit the file "/etc/mysql/mysql.conf.d/mysqld.cnf" and comment out the line of "bind-address" to:
|
|---|
| 78 |
|
|---|
| 79 | #bind-address = 127.0.0.1
|
|---|
| 80 |
|
|---|
| 81 | - Restart MySQL:
|
|---|
| 82 |
|
|---|
| 83 | shell> sudo /etc/init.d/mysql restart
|
|---|
| 84 |
|
|---|
| 85 | - On your local machine:
|
|---|
| 86 |
|
|---|
| 87 | shell> mysql -u mytestuser -h [EC2_IP_ADDR] -p
|
|---|
| 88 |
|
|---|