天天看點

Mysql在Linux中的簡單配置

1.啟動Mysql服務指令:

  [[email protected] ~]# service musqld start

2.登入資料庫,預設的使用者名是root,沒有密碼. 輸入mysql後,直接按Enter鍵操作資料庫:

  [[email protected] ~]# mysql

  mysql>

3.設定root的密碼:

  [[email protected] ~]# mysqladmin -u root password "pwd"

4.使用密碼登入資料庫:

  [[email protected] ~]# mysql -u root -ppwd

  mysql>

5.設定資料庫的編碼格式為utf-8:

  --檢視mysql的預設編碼:

  mysql>show variables like 'character%'

  +--------------------------+----------------------------+

  | Variable_name | Value |

  +--------------------------+----------------------------+

  | character_set_client | latin1 |

  | character_set_connection | latin1 |

  | character_set_database | latin1 |

  | character_set_filesystem | binary |

  | character_set_results | latin1 |

  | character_set_server | latin1 |

  | character_set_system | utf8 |

  | character_sets_dir | /usr/share/mysql/charsets/ |

  +--------------------------+----------------------------+

  --修改mysql的主要配置檔案/etc/my.cnf檔案. (如果/etc/目錄下沒有my.cnf檔案,需要到/usr/share/mysql/下找到*.cnf檔案,拷貝其中一個到/etc/并改名為my.cnf)中. 指令如下:

  [[email protected] ~]# cp /usr/share/mysql/my-medium.cnf  /etc/my.cnf       注:拷貝*.cnf檔案到/etc/并改名為my.cnf

  --編輯my.cnf檔案,添加如下所示紅色代碼

  [[email protected] ~]# vi /etc/my.cnf

  [client]

  default-character-set=utf8

  [mysqld]

  datadir=/var/lib/mysql

  socket=/var/lib/mysql/mysql.sock

  user=mysql

  lower_case_table_names=1

  # Disabling symbolic-links is recommended to prevent assorted security risks

  symbolic-links=0

  character-set-server=utf8

  init_connect='SET NAMES utf8'

  [mysql]

  no-auto-rehash

  default-character-set=utf8

  [mysqld_safe]

  log-error=/var/log/mysqld.log

  pid-file=/var/run/mysqld/mysqld.pid

  --退出編輯指令:按Esc鍵,輸入: 号,再輸入x 儲存并退出.這樣資料庫的編碼格式就設定好了.

  --重新開機mysql服務:

  [[email protected] ~]# service mysqld restart

  [[email protected] ~]# mysql -u root -ppwd

  mysql>show variables like 'character%'

  +--------------------------+----------------------------+

  | Variable_name | Value |

  +--------------------------+----------------------------+

  | character_set_client | utf8 |

  | character_set_connection | utf8 |

  | character_set_database | utf8 |

  | character_set_filesystem | binary |

  | character_set_results | utf8 |

  | character_set_server | utf8 |

  | character_set_system | utf8 |

  | character_sets_dir | /usr/share/mysql/charsets/ |

  +--------------------------+----------------------------+

6.建立使用者:

  mysql> insert into mysql.user(Host,User,Password) values ('localhost','name',password('pwd'));

  mysql> flush privileges; 注:重新整理權限

7.建立資料庫,庫名為dbname:

  mysql> create database dbname;

8.将操作資料庫dbname的所有權限配置設定給name使用者:

  mysql> grant all privileges on dbname.* to name@'%' identified by 'pwd';   注:這裡的 '%' 可以用localhost代替,'%'的好處是遠端機器可以用該使用者通路

  mysql> flush privileges;

  mysql>\q        注:退出mysql

9.用root登入mysql,删除其他使用者:

  mysql> DELETE FROM user WHERE User='name' and Host='localhost';  注:删除使用者

  mysql>  drop database dbname;  注:删除資料庫

10.開放mysql的3306端口,使遠端機器可以通路.

  --首先确定要遠端使用者的host是否是%,如果不是請用root登入mysql并設定host為%

  mysql> update mysql.user set host = '%' where user = 'name';

  mysql> select host ,user from mysql.user;

  --打開防火牆檔案,增加如下紅色行:

  [[email protected] ~]# vi  /etc/sysconfig/iptables

  # Firewall configuration written by system-config-firewall

  # Manual customization of this file is not recommended.

  *filter

  :INPUT ACCEPT [0:0]

  :FORWARD ACCEPT [0:0]

  :OUTPUT ACCEPT [0:0]

  -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

  -A INPUT -p icmp -j ACCEPT

  -A INPUT -i lo -j ACCEPT

  -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

  -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

  -A INPUT -j REJECT --reject-with icmp-host-prohibited

  -A FORWARD -j REJECT --reject-with icmp-host-prohibited

  COMMIT

  --重新開機防火牆:

  [[email protected] ~]# service  iptables restart