Как обеспечить удаленный доступ к MySQL для root
Итак мы хотим, чтобы root мог влезть в любую базу данных откуда угодно (просто рай для хакеров).
Во-первых, добавляем привилегии root
1 |
grant all privileges on *.* to 'root'@'%' identified by 'secret-password' |
Во-вторых, комментируем строчку в /etc/mysql/mysql.conf.d/mysqld.cnf
1 |
#bind-address = 127.0.0.1 |
В третьих, перезапускаeм MySQL
1 |
/etc/init.d/mysql restart |
Удаленный доступ работает (ниже пример, взятый с бескрайних просторов)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import java.sql.Statement; /** * Simple Java program to connect to MySQL database running on localhost and * running SELECT and INSERT query to retrieve and add data. * @author Javin Paul */ public class MySQL { // JDBC URL, username and password of MySQL server private static final String url = "jdbc:mysql://192.168.1.100:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Moscow"; private static final String user = "root"; private static final String password = "password111"; // JDBC variables for opening and managing connection private static Connection con; private static Statement stmt; private static ResultSet rs; public void test() { String query = "select count(*) from books"; try { // opening database connection to MySQL server con = DriverManager.getConnection(url, user, password); // getting Statement object to execute query stmt = con.createStatement(); // executing SELECT query rs = stmt.executeQuery(query); while (rs.next()) { int count = rs.getInt(1); System.out.println("Total number of books in the table : " + count); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } finally { //close connection ,stmt and resultset here try { con.close(); } catch(SQLException se) { /*can't do anything */ } try { stmt.close(); } catch(SQLException se) { /*can't do anything */ } try { rs.close(); } catch(SQLException se) { /*can't do anything */ } } } } |