Task: MySQL Server Remote Access
Change the ip address to public ip of server
/etc/mysql/my.cnf
Task: MySQL Server Remote Access
Change the ip address to public ip of server
/etc/mysql/my.cnf
1 |
Sample Setup |
1 2 3 4 5 6 7 8 9 10 11 12 |
+----------- server1 192.168.1.6 | +------------ tomcat1 192.168.1.7 | +------------------+ | | MySQL Server | --------+------------ apache2 192.168.1.8 | 192.168.1.5:3306 | --------+ +------------------+ | LAN 192.168.1.0/24 +------------- pc1 192.168.1.51 | | +-------------- pc25 192.168.1.76 |
You need to allow access to 192.168.1.5 from apache server located at 192.168.1.8.
Step #1: Configure MySQL Server For Remote Access
1 2 3 4 5 |
$ ssh root@192.168.1.5 [/bash] Edit /etc/my.cnf, enter: # vi /etc/my.cnf |
Modify or append as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# make sure the following line is deleted or commented out # skip-networking bind-address = 192.168.1.5 # ------------------ # Save and close the file. Restart the mysql server, enter: # service mysqld restart Verify that the TCP port 3306 is open, enter: Make Sure TCP Port # 3306 is Opened For Business # netstat -tulpn | grep :3306 |
Step #2: Linux Firewall Configuration For TCP Port # 3306
# >>>> You need to open TCP port # 3306 at the firewall level, enter:
1 2 3 4 5 6 7 8 9 10 11 |
# iptables -A INPUT -i eth0 -s 192.168.1.8 -p tcp --destination-port 3306 -j ACCEPT # service iptables save Step #3: Configure Database Remote Access # mysql -u root -p mysql mysql> update db set Host='192.168.1.8' where Db='salesdb'; mysql> update user set Host='192.1681.8' where user='foo'; mysql> \q # Login to 192.168.1.8 and type the following command to test mysql server remote access: $ mysql -u foo -h 192.168.1.5 -p salesdb |
Sample outputs:
1 2 3 4 5 6 7 |
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27720995 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> |
başlat/ çalıştır ,
CONTROL USERPASSWORDS2
How Do I Enable Remote Access To MySQL Database Server?
You need type the following commands which will allow remote connections.
Step # 1: Login Using SSH (if server is outside your data center)
First, login over ssh to remote MySQL database server:
1 2 |
ssh user@server1.cyberciti.biz Step # 2: Edit my.cnf File |
Once connected you need to edit the MySQL server configuration file my.cnf using a text editor such as vi.
1 2 3 |
If you are using Debian Linux file is located at /etc/mysql/my.cnf location If you are using Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location If you are using FreeBSD you need to create a file /var/db/mysql/my.cnf |
1 2 |
Edit /etc/my.cnf, run: # vi /etc/my.cnf |
Step # 3: Once file opened, locate line that read as follows
1 2 3 4 5 |
[mysqld] Make sure line skip-networking is commented (or remove line) and add following line bind-address=YOUR-SERVER-IP For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows: |
1 2 3 4 5 6 7 8 9 10 11 |
[mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp language = /usr/share/mysql/English bind-address = 65.55.55.2 # skip-networking |
Where,
bind-address : IP address to bind to.
skip-networking : Don’t listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should be removed from my.cnf or put it in comment state.
Step# 4 Save and Close the file
1 2 3 4 5 6 7 8 9 10 11 12 |
If you are using Debian / Ubuntu Linux, type the following command to restart the mysql server: # /etc/init.d/mysql restart If you are using RHEL / CentOS / Fedora / Scientific Linux, type the following command to restart the mysql server: # /etc/init.d/mysqld restart If you are using FreeBSD, type the following command to restart the mysql server: # /usr/local/etc/rc.d/mysql-server stop # /usr/local/etc/rc.d/mysql-server start OR # /usr/local/etc/rc.d/mysql-server restart |
Step # 5 Grant access to remote IP address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Connect to mysql server: $ mysql -u root -p mysql Grant access to a new database If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you need to type the following commands at mysql> prompt: mysql> CREATE DATABASE foo; mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD'; How Do I Grant Access To An Existing Database? Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin, To grant access to this IP address type the following command At mysql> prompt for existing database, enter: mysql> update db set Host='202.54.10.20' where Db='webdb'; mysql> update user set Host='202.54.10.20' where user='webadmin'; |
Step # 6: Logout of MySQL
Type exit command to logout mysql:
mysql> exit
Step # 7: Open port 3306
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
You need to open TCP port 3306 using iptables or BSD pf firewall. A sample iptables rule to open Linux iptables firewall /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT OR only allow remote connection from your web server located at 10.5.1.3: /sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT OR only allow remote connection from your lan subnet 192.168.1.0/24: /sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT Finally save all rules (RHEL / CentOS specific command): # service iptables save A sample FreeBSD / OpenBSD pf rule ( /etc/pf.conf) pass in on $ext_if proto tcp from any to any port 3306 OR allow only access from your web server located at 10.5.1.3: pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306 flags S/SA synproxy state |
Step # 8: Test it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
From your remote system or your desktop type the following command: $ mysql -u webadmin –h 65.55.55.2 –p Where, -u webadmin: webadmin is MySQL username -h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN) -p : Prompt for password You can also use the telnet or nc command to connect to port 3306 for testing purpose: $ echo X | telnet -e X 65.55.55.2 3306 OR $ nc -z -w1 65.55.55.2 3306 Sample outputs: Connection to 65.55.55.2 3306 port [tcp/mysql] succeeded! |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide] Производитель и Потребитель буфера ПРОИЗВОДИТЕЛЬ БУФЕРА, prod_onebuf.c : /* producer program: producer program sets up a buffer to be read by a consumer semaphores are used to ensure that producer doesn't overwrite an unread buffer and consumer doesn't the same data more than once. */ #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #define MSG_SIZE 30 #define MAX 5 main() { key_t key; /* two semaphores in a set index 0 -incremented when producer has reset buffer -tested and decremented by consumer to check if buffer has been reset index 1 -incremented when consumer has read buffer -tested and decremented by producer to check if consumer is done */ static struct sembuf wait_consumed = { 1, -1, 0}; static struct sembuf signal_produced = { 0, 1, 0}; int semid, shmid, i; char *message; if((key = ftok( getenv("HOME"), 'u')) == -1){ fprintf(stderr,"ftok key formation error\n"); exit(1); } if((semid=semget(key, 2, IPC_CREAT | 0660)) == -1){ perror("producer semget():"); exit(2); } if((shmid=shmget(key, MSG_SIZE, IPC_CREAT | 0660)) == -1) { perror("producer shmget():"); exit(3); } message=shmat(shmid, 0, 0); for(i=1; i < MAX; i++) { /* producer has to go first */ if(i > 1) semop(semid,&wait_consumed,1); sprintf(message, "message %d", i); semop(semid, &signal_produced, 1); } shmdt(message); sleep(5); /* allow consumer to digest last message */ /* alternatively a DELIMITER string could be placed in shared memory when seen by the consumer, it would exit. the producer would do shmctl(...,IPC_STAT,...) and when shm_attach==1 it would remove the two IPC facilities */ shmctl(shmid, IPC_RMID, NULL); semctl(semid, 0, IPC_RMID ,0); } NОТРЕБИТЕЛЬ БУФЕРА, cons_onebuf.c : /* consumer program: producer program sets up a buffer to be read by a consumer. semaphores are used to ensure that producer doesn't overwrite an unread buffer and consumer doesn't read the same data more than once. */ #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #define MSG_SIZE 30 main(int argc, char *argv[]) { key_t key; /* two semaphores in a set index 0 - incremented when producer has reset buffer - tested and decremented by consumer to check if buffer has been reset index 1 - incremented when consumer has read buffer - tested and decremented by producer to check if consumer is done */ static struct sembuf wait_produced = { 0, -1, 0}; static struct sembuf signal_consumed = { 1, 1, 0}; int semid, shmid, rtn; char *message; if((key = ftok( getenv("HOME"), 'u')) == -1){ fprintf(stderr,"ftok key formation error\n"); exit(1); } /* either producer or consumer might be the creator but producer will be the remover of the IPC resources. producer and consumer's effective uid must be the same. */ if ((semid=semget(key, 2, IPC_CREAT | 0660)) == -1) { perror("consumer semget():"); exit(2); } if ((shmid=shmget(key, MSG_SIZE, IPC_CREAT | 0660)) == -1) { perror("producer shmget():"); exit(3); } message=shmat(shmid, (char *)0, SHM_RDONLY); while(1){ rtn=semop(semid,&wait_produced,1); /* when producer is done semid will be IPC_RMID forcing break */ if(rtn == -1){ perror("consumer - semop on wait_consumed"); break; } printf("%s received: %s\n", argv[0], message); semop(semid, &signal_consumed, 1); } shmdt(message); exit(0); } ВЫЗОВ: $ prod_onebuf& 14649 $ cons_onebuf cons_onebuf received: message 1 cons_onebuf received: message 2 cons_onebuf received: message 3 cons_onebuf received: message 4 consumer-semop on wait_consumed:Identifier removed |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
[c] #include <stdio.h> #include <iostream> #include <cstdio> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <string> #include <vector> #include <cstring> #include <cstdlib> #include<fstream> #include <time.h> // Kutay ZORLU using namespace std; void readfile(); vector<string> satirlar(9); vector<string> sonuc(9); vector <int> A(16); vector<int > B(16); vector<int > C(16); int main (){ string str = "SOMEload"; double start = clock(); bool okudumu=false; int fork_id = fork(); while (!okudumu) { switch (fork_id) { // forkun durumuna gore asagidaki kodlar gerceklestirilecek case -1: // -1 hata durumu idi perror("fork"); return 3; default: if(!okudumu){ readfile(); okudumu = true; } else{ cout << "babadayım "<<endl; //wait(0); // let the child finish C[0] = A[0]*B[0]+ A[1]*B[4] + A[2]*B[8] + A[3]*B[12]; C[1] = A[0]*B[1]+ A[1]*B[5] + A[2]*B[9] + A[3]*B[13]; C[2] = A[0]*B[2]+ A[1]*B[6] + A[2]*B[10] + A[3]*B[14]; C[3] = A[0]*B[3]+ A[1]*B[7] + A[2]*B[11] + A[3]*B[15]; C[4] = A[4]*B[0]+ A[5]*B[4] + A[6]*B[8] + A[7]*B[12]; C[5] = A[4]*B[1]+ A[5]*B[5] + A[6]*B[9] + A[7]*B[13]; C[6] = A[4]*B[2]+ A[5]*B[6] + A[6]*B[10] + A[7]*B[14]; C[7] = A[4]*B[3]+ A[5]*B[7] + A[6]*B[11] + A[7]*B[15]; cout << "DEFAULT 1 içindeuim"<< endl; } break; // break olana kadar default calisiyor case 0: //bebe cout << "CASE 0 içindeyim"<< endl; if(okudumu){ cout << "C carpti" ; C[8] = A[8]*B[0]+ A[9]*B[4] + A[10]*B[8] + A[11]*B[12]; C[9] = A[8]*B[1]+ A[9]*B[5] + A[10]*B[9] + A[11]*B[13]; C[10] = A[8]*B[2]+ A[9]*B[6] + A[10]*B[10] + A[11]*B[14]; C[11] = A[8]*B[3]+ A[9]*B[7] + A[10]*B[11] + A[11]*B[15]; C[12] = A[12]*B[0]+ A[13]*B[4] + A[14]*B[8] + A[15]*B[12]; C[13] = A[12]*B[1]+ A[13]*B[5] + A[14]*B[9] + A[15]*B[13]; C[14] = A[12]*B[2]+ A[13]*B[6] + A[14]*B[10] + A[15]*B[14]; C[15] = A[12]*B[3]+ A[13]*B[7] + A[14]*B[11] + A[15]*B[15]; } break; } } // // // cout << str << endl; // double finish = clock(); int sure_time = (double(finish-start)/CLOCKS_PER_SEC); cout << "SURE : "<<sure_time << endl; return 1; } void readfile(){ ifstream myfile,myfile_2; myfile.open ("veri.dat"); // char output[100]; int i=0; if (myfile.is_open()) { while (!myfile.eof()) { // myfile.getline(output,100) ; cout << output << "[" << i << "]" <<endl; i++; } } myfile.close(); myfile_2.open ("veri.dat"); // i=0; char xx[100]; if (myfile_2.is_open()) { while (!myfile_2.eof()) { if(i<1) { myfile_2 >> xx;} else { myfile_2 >> A[i-1]; cout << "A["<<i-1 << "]:" << A[i-1] <<endl; if((i-1)>15) { B[i-17] = A[i-1]; //b2 4 cout <<"B["<<i-17<< "]" << B[i-17]<< endl; } } i++; } } myfile_2.close(); //myfile << "Writing this to a file.\n"; } [/c] |
1 2 3 4 5 6 7 8 |
class xxxx : public QAbstractItemDelegate { public: xxxx( QObject *parent = 0 ); void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const; }; |