【MySQL】RHEL6.5下MySQ編譯安裝、簡單的MySQL操做

軟件下載:
node

MySQL-5.5.40.tar.gz:http://down.51cto.com/data/1966628 mysql

cmake-2.8.0(MySQL編譯工具):http://down.51cto.com/data/1966627linux

配置防火牆,開啓3306端口、selinuxc++

安裝編譯工具sql

[root@localhost src]# yum install -y gcc gcc-c++

安裝cmake數據庫

[root@localhost src]# tar zxvf cmake-2.8.0.tar.gz 
[root@localhost src]# cd cmake-2.8.0
[root@localhost cmake-2.8.0]# ./configure 
[root@localhost cmake-2.8.0]# make && make install

編譯安裝MySQLexpress

[root@localhost cmake-2.8.0]# cd ..
[root@localhost src]# tar zxvf mysql-5.5.40.tar.gz 
[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -g mysql mysql -s /sbin/nologin 
[root@localhost src]# mkdir -p /data/mysql       
[root@localhost src]# chown -R mysql:mysql /data/mysql
[root@localhost src]# cd mysql-5.5.40
[root@localhost mysql-5.5.40]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
.....
-- Could NOT find Curses  (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:83 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
[root@localhost mysql-5.5.40]# rm CMakeCache.txt 
rm: remove regular file `CMakeCache.txt'? y   
[root@localhost mysql-5.5.40]# yum -y install ncurses-devel
[root@localhost mysql-5.5.40]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
......
Warning: Bison executable not found in PATH
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mysql-5.5.40
[root@localhost mysql-5.5.40]# yum -y install Bison
[root@localhost mysql-5.5.40]# make && make install
[root@localhost mysql-5.5.40]# rm -rf /etc/my.cnf   #刪除系統默認的配置文件(若是默認沒有就不用刪除)
[root@localhost mysql-5.5.40]# cd /usr/local/mysql/
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql   #生成mysql系統數據庫
[root@localhost mysql]# ln -s /usr/local/mysql/my.cnf /etc/my.cnf
[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld    #把Mysql加入系統啓動
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# vim /etc/rc.d/init.d/mysqld 
 46 basedir=/usr/local/mysql
 47 datadir=/data/mysql
 [root@localhost mysql]# vim /etc/profile    #把mysql服務加入系統環境變量:在最後添加下面這一行
 export PATH=$PATH:/usr/local/mysql/bin
 [root@localhost mysql]# source /etc/profile

把myslq的庫文件連接到系統默認的位置,這樣在編譯相似PHP等軟件時能夠不用指定mysql的庫文件地址。vim

[root@localhost mysql]# ln -s /usr/local/mysql/lib/mysql /lib/mysql
[root@localhost mysql]# ln -s /usr/local/include/mysql /usr/include/mysql
[root@localhost mysql]# mkdir /var/lib/mysql
[root@localhost mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

service mysqld startAnother MySQL daemon already running with the same unix socket.windows

臨時解決方法:刪除或者更名:/var/lib/mysql/mysql.sock bash

yum安裝

[root@localhost ~]# yum -y install mysql mysql-server  mysql-devel


登錄mysql

[root@localhost ~]# reboot
[root@localhost ~]# mysql -u root
[root@OTRS mysql]# mysql -uroot -h127.0.0.1 -pjustin -P3306

默認的mysql是沒密碼的

設置Mysql密碼

[root@localhost ~]# mysqladmin -u root password 'justin'
[root@localhost ~]# mysql -u root -p password 'justin'

mysqladmin就是用來設置密碼的工具,-u 指定用戶,passwod 後跟要定義的密碼,密碼須要用單引號或者雙引號括起來。若是你沒在系統變量$PATH中指定/usr/local/mysql/bin/這個目錄,須要在命令前加/usr/local/mysql/bin/

或者如下命令設定改密碼:

[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y    --設置root密碼
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y    --移除匿名用戶
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y  --不容許遠程經過root登錄
 ... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y  --移除測試數據庫
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y    --從新加載特權表
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
[root@localhost ~]#

怎麼修改數據庫密碼?

[root@localhost ~]# mysqladmin -u root -p password "justin"
Enter password:
[root@localhost ~]#

"justin"爲須要修改成的密碼,Enter password後輸入的是原來的密碼

    遺忘數據庫密碼?

a、修改MySQL配置文件,在[mysqld]下任意位置添加一行skip-grant-tables」並重啓mysql服務

[root@localhost ~]# cp /etc/my.cnf /etc/my.cnfbak
[root@localhost ~]# vim /etc/my.cnf
25 # The MySQL server
26 [mysqld]
27 port            = 3306
28 socket          = /var/lib/mysql/mysql.sock
29 skip-grant-tables
30 skip-locking
31 key_buffer_size = 16M
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@localhost ~]#

      b、登錄mysql修改root密碼

[root@localhost ~]# mysql -uroot -p
Enter password:   --直接回車
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection idis 2
Server version: 5.1.71-log Source distribution
Copyright (c) 2000, 2013, Oracle and/oritsaffiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/orits
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;'or '\h'forhelp. Type '\c'to clearthe current input statement.
mysql> use mysql;   --使用mysql數據庫
Database changed
mysql> update user setpassword=password("newpassword") where user="root";   --newpassword爲新的root密碼
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0
mysql> flush privileges;   --刷新特權表,不須要重啓服務就能夠是剛纔的操做生效
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@localhost ~]#

 c、取消剛纔在/etc/my.cnf中添加的skip-grant-tables並重啓服務後使用newpassword成功登錄mysql

[root@localhost ~]# vim /etc/my.cnf
26 [mysqld]
27 port            = 3306
28 socket          = /var/lib/mysql/mysql.sock
29 #kip-grant-tables
30 skip-locking
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection idis 2
Server version: 5.1.71-log Source distribution
Copyright (c) 2000, 2013, Oracle and/oritsaffiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/orits
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;'or '\h'forhelp. Type '\c'to clearthe current input statement.
mysql>

修改指定用戶密碼:

mysql> update mysql.user set password=password('123') where User="user1" and host="localhost";
Query OK, 0 rows affected (0.12 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges;

例如:若是忘記了zabbix的登錄帳戶admin密碼,也能夠經過如下步驟找回:

[root@localhost ~]# mysql -uroot -p      #登錄數據庫
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> show databases;                   #查詢當前全部的庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+
4 rows in set (0.02 sec)
 
mysql> use zabbix;                  #進入zabbix數據庫
Database changed
mysql> show tables;                 #再查看一下里面有不少張表,admin密碼放在users表裏面。
+-----------------------+
| Tables_in_zabbix      |
+-----------------------+
| acknowledges          |
| actions               |
| alerts                |
| application_template  |
| applications          |
| auditlog              |
| auditlog_details      |
| autoreg_host          |
| conditions            |
| config                |
| dbversion             |
| dchecks               |
| dhosts                |
| drules                |
| dservices             |
| escalations           |
| events                |
| expressions           |
| functions             |
| globalmacro           |
| globalvars            |
| graph_discovery       |
| graph_theme           |
| graphs                |
| graphs_items          |
| group_discovery       |
| group_prototype       |
| groups                |
| history               |
| history_log           |
| history_str           |
| history_str_sync      |
| history_sync          |
| history_text          |
| history_uint          |
| history_uint_sync     |
| host_discovery        |
| host_inventory        |
| hostmacro             |
| hosts                 |
| hosts_groups          |
| hosts_templates       |
| housekeeper           |
| httpstep              |
| httpstepitem          |
| httptest              |
| httptestitem          |
| icon_map              |
| icon_mapping          |
| ids                   |
| images                |
| interface             |
| interface_discovery   |
| item_discovery        |
| items                 |
| items_applications    |
| maintenances          |
| maintenances_groups   |
| maintenances_hosts    |
| maintenances_windows  |
| mappings              |
| media                 |
| media_type            |
| node_cksum            |
| nodes                 |
| opcommand             |
| opcommand_grp         |
| opcommand_hst         |
| opconditions          |
| operations            |
| opgroup               |
| opmessage             |
| opmessage_grp         |
| opmessage_usr         |
| optemplate            |
| profiles              |
| proxy_autoreg_host    |
| proxy_dhistory        |
| proxy_history         |
| regexps               |
| rights                |
| screens               |
| screens_items         |
| scripts               |
| service_alarms        |
| services              |
| services_links        |
| services_times        |
| sessions              |
| slides                |
| slideshows            |
| sysmap_element_url    |
| sysmap_url            |
| sysmaps               |
| sysmaps_elements      |
| sysmaps_link_triggers |
| sysmaps_links         |
| timeperiods           |
| trends                |
| trends_uint           |
| trigger_depends       |
| trigger_discovery     |
| triggers              |
| user_history          |
| users                 |                   #users表
| users_groups          |
| usrgrp                |
| valuemaps             |
+-----------------------+
108 rows in set (0.00 sec)
 
mysql> select * from users;       # 查看錶裏的字段,admin對應的ID是1
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
| userid | alias | name   | surname       | passwd                           | url | autologin | autologout | lang  | refresh | type | theme   | attempt_failed | attempt_ip | attempt_clock | rows_per_page |
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
|      1 | Admin | Zabbix | Administrator | 5fce1b3e34b520afeffb37ce08c7cd66 |     |         1 |          0 | zh_CN |      30 |    3 | default |              0 |            |             0 |            50 |
|      2 | guest |        |               | d41d8cd98f00b204e9800998ecf8427e |     |         0 |        900 | en_GB |      30 |    1 | default |              0 |            |             0 |            50 |
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
2 rows in set (0.00 sec)
 
mysql> select userid,passwd from users;
+--------+----------------------------------+
| userid | passwd                           |
+--------+----------------------------------+
|      1 | 5fce1b3e34b520afeffb37ce08c7cd66 |       #密碼是加密的  
|      2 | d41d8cd98f00b204e9800998ecf8427e |
+--------+----------------------------------+
2 rows in set (0.00 sec)
 
mysql>

從新開個終端,生成一個MD5加密的密碼,這裏密碼設置的是redhat

[root@localhost ~]# echo -n redhat|openssl md5    #-n就表示不輸入回車符,不加-n,不然就不是這個結果了。
(stdin)= e2798af12a7a0f4f70b4d69efbc25f4d
[root@localhost ~]#

接着上面的爲admin用戶設定一個密碼

mysql> update users set passwd='e2798af12a7a0f4f70b4d69efbc25f4d' where userid = '1';
       #或者直接使用update  users set passwd=md5("redhat") where userid='1';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
 
mysql> quit
Bye
[root@localhost ~]#


mysql基本操做

查詢當前全部的庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.03 sec)

mysql>

查詢某個庫的表

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

mysql>

 查看某個表的字段

mysql> desc func;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| name  | char(64)                     | NO   | PRI |         |       |
| ret   | tinyint(1)                   | NO   |     | 0       |       |
| dl    | char(128)                    | NO   |     |         |       |
| type  | enum('function','aggregate') | NO   |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>

查看某個表的表結構(建立表時的詳細結構)

mysql> show create table func;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                   |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| func  | CREATE TABLE `func` (
  `name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
  `ret` tinyint(1) NOT NULL DEFAULT '0',
  `dl` char(128) COLLATE utf8_bin NOT NULL DEFAULT '',
  `type` enum('function','aggregate') CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='User defined functions' |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

查看當前是哪一個用戶

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql>

查看當前所在數據庫

mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql>

建立一個新庫

mysql> create database db1;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql>

建立一個表

mysql> create table t1 ( `id` int(4), `name` char(40));
Query OK, 0 rows affected (0.02 sec)

mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql>

刪除某張表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db1;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> drop database db1;
Query OK, 0 rows affected (0.23 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.03 sec)

mysql>

刪除某個庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| otrs               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database otrs;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

查看當前數據庫版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.40    |
+-----------+
1 row in set (0.00 sec)

mysql>

查看當前系統時間

mysql> select current_date,current_time;
+--------------+--------------+
| current_date | current_time |
+--------------+--------------+
| 2014-12-24   | 09:04:01     |
+--------------+--------------+
1 row in set (0.00 sec)

mysql>

查看當前mysql的狀態

mysql> show status;
+------------------------------------------+-------------+
| Variable_name                            | Value       |
+------------------------------------------+-------------+
| Aborted_clients                          | 0           |
| Aborted_connects                         | 0           |
| Binlog_cache_disk_use                    | 0           |
| Binlog_cache_use                         | 0           |

建立一個普通用戶並受權

mysql> grant all on *.* to user1 identified by 'justin';                                           
Query OK, 0 rows affected (0.03 sec)

mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>

all 表示全部的權限(讀、寫、查詢、刪除等等操做),*.*前面的*表示全部的數據庫,後面的*表示該數據庫全部的表,identified by 後面跟密碼,用單引號括起來。這裏的user1指的是localhost上的user1,若是是給網絡上的其餘機器上的某個用戶受權則這樣:

mysql> grant all on *.* to 'user2'@'10.15.24.245' identified by 'justin';
Query OK, 0 rows affected (0.00 sec)
mysql>

用戶和主機的IP之間有一個@,另外主機IP那裏能夠用%替代,表示全部主機

mysql> grant all on *.* to 'user3'@'%' identified by 'justin';
Query OK, 0 rows affected (0.02 sec)

mysql>grant select,delete,update,create,drop on *.* to 'user4'@'%' identified by '123';

數據庫/數據表/數據列權限: Alter: 修改已存在的數據表(例如增長/刪除列)和索引。
Create: 創建新的數據庫或數據表。
Delete: 刪除表的記錄。
Drop: 刪除數據表或數據庫。
INDEX: 創建或刪除索引。
Insert: 增長表的記錄。
Select: 顯示/搜索表的記錄。
Update: 修改表中已存在的記錄。

全局管理MySQL用戶權限:

file: 在MySQL服務器上讀寫文件。
PROCESS: 顯示或殺死屬於其它用戶的服務線程。
RELOAD: 重載訪問控制表,刷新日誌等。
SHUTDOWN: 關閉MySQL服務。

特別的權限:

ALL: 容許作任何事(和root同樣)。
USAGE: 只容許登陸--其它什麼也不容許作

查看某用戶權限:

mysql> show grants for user1;
+---------------------------------------------------------------------------------------------------------------+
| Grants for user1@%                                                                                            |
+---------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' IDENTIFIED BY PASSWORD '*418F5110126E965257925334DE2CECD97AE332B5' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

刪除某個用戶:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delete from user  where user='user1' and host='localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
相關文章
相關標籤/搜索