软件下载:
MySQL-5.5.40.tar.gz:http://down.51cto.com/data/1966628
cmake-2.8.0(MySQL编译工具):http://down.51cto.com/data/1966627
配置防火墙,开启3306端口、selinux
安装编译工具
[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
安装MySQL
[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的库文件地址。
[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
登陆mysql
[root@localhost ~]# reboot [root@localhost ~]# mysql -u root
默认的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> 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;