仅限 centos7如下 版本css
#yum install mysql #yum install mysql-server #yum install mysql-devel
启动服务python
[root@localhost hadoop]# service mysqld restart
centos 7 mysql-server失败mysql
[root@yl-web yl]# yum install mysql-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.sina.cn * extras: mirrors.sina.cn * updates: mirrors.sina.cn No package mysql-server available. Error: Nothing to do
查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。ios
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL受权许可。开发这个分支的缘由之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,所以社区采用分支的方式来避开这个风险。MariaDB的目的是彻底兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。web
解决:安装mariadbsql
[root@yl-web yl]# yum install mariadb-server mariadb mariadb数据库的相关命令是: systemctl start mariadb #启动MariaDB systemctl stop mariadb #中止MariaDB systemctl restart mariadb #重启MariaDB systemctl enable mariadb #设置开机启动 因此先启动数据库 [root@yl-web yl]# systemctl start mariadb
链接mysqlshell
[root@localhost hadoop]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2. 配置mysql
设置密码数据库
mysql> set password for 'root'@'localhost' =password('password'); Query OK, 0 rows affected (0.00 sec) mysql> //不须要重启数据库便可生效。
设置编码swift
mysql配置文件为/etc/my.cnf
最后加上编码配置
[mysql]
default-character-set =utf8 //这里的字符编码必须和/usr/share/mysql/charsets/Index.xml中一致。
远程链接设置centos
把在全部数据库的全部表的全部权限赋值给位于全部IP地址的root用户。
mysql> grant all privileges on *.* to root@'%'identified by 'password'; 若是是新用户而不是root,则要先新建用户 mysql>create user 'username'@'%' identified by 'password'; 此时就能够进行远程链接了。
配置mysql 端口号
[root@test etc]# vi my.cnf [mysqld] port=3306 // 加上设置的端口号 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid "my.cnf" 11L, 261C written [root@test etc]# 4. 从新启动mysql [root@localhost /]# service mysqld restart
查看端口:
mysql> show global variables like 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.00 sec) mysql>
远程链接可能还会存在防火墙阻断远程链接失败的状况
加入对应mysql端口的 容许
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT 结果: [root@localhost /]# 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 -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT COMMIT 重启防火墙 [root@localhost /]# service iptables restart
若是链接失败 或者 telnet ip port 失败 则关闭防火墙
service iptables stop
// 服务器重启将会失效
主从复制配置
一、主从服务器分别做如下操做:
- 1.一、版本一致
- 1.二、初始化表,并在后台启动mysql
- 1.三、修改root的密码
二、修改主服务器master
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 server-id=222 //[必须]服务器惟一ID,默认是1,通常取IP最后一段
三、修改从服务器slave:
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必须]启用二进制日志 server-id=226 //[必须]服务器惟一ID,默认是1,通常取IP最后一段
四、重启两台服务器的mysql
/etc/init.d/mysql restart
五、在主服务器上创建账户并受权slave:
#/usr/local/mysql/bin/mysql -uroot -pmttang
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //通常不用root账号,“%”表示全部客户端均可能连,只要账号,密码正确,此处可用具体客户端IP代替,如192.168.1 45.226,增强安全。
六、登陆主服务器的mysql,查询master的状态
mysql>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 308 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 注:执行完此步骤后不要再操做主服务器MYSQL,防止主服务器状态值变化
七、从服务器设置普通用户只读模式
mysql>create user 'test'@'%' identified by '123456'; // 建立普通用户,能够远程链接 mysql> grant select on *.* to test@'%'identified by '123456'; //受权全部库,只能查询操做 mysql> grant all privileges on *.* to test@'%'identified by '123456'; //这是授予全部权限
- 关于mysql建立用户以及权限,下面有详细说明;这里只是作了主从复制避免从库添加数据作准备;
- 从库远程登陆就用刚才设置的普通的只要查询权限的帐号去登陆,避免致使主从出错
八、配置从服务器Slave:
mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456', master_log_file='mysql-bin.000004',master_log_pos=308; //注意不要断开,308数字先后无单引号。 Mysql>start slave; //启动从服务器复制功能 change master to master_host='192.168.80.131',master_user='mysync',master_password='q123456',master_log_file='mysql-bin.000001',master_log_pos=251;
九、检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服务器地址 Master_User: mysync //受权账户名,尽可能避免使用root Master_Port: 3306 //数据库端口,部分版本没有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此状态必须YES Slave_SQL_Running: Yes //此状态必须YES ...... 注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,不然都是错误的状态(如:其中一个NO均属错误)。 以上操做过程,主从服务器配置完成。
十、MySQL添加用户、删除用户与受权详细说明;
- 1.新建用户
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234")); 这样就建立了一个名为:test 密码为:1234 的用户。 注意:此处的"localhost",是指该用户只能在本地登陆,不能在另一台机器上远程登陆。 若是想远程登陆的话, 将"localhost"改成"%",表示在任何一台电脑上均可以登陆。也能够指定某台机器能够远程登陆。
- 2.为用户受权
受权格式:grant 权限 on 数据库.* to 用户名@登陆主机 identified by "密码"; 2.1 登陆MYSQL(有ROOT权限),这里以ROOT身份登陆: @>mysql -u root -p @>密码
2.2 首先为用户建立一个数据库(testDB): mysql>create database testDB;
2.3 受权test用户拥有testDB数据库的全部权限(某个数据库的全部权限): mysql>grant all privileges on testDB.* to test@localhost identified by '1234'; mysql>flush privileges;//刷新系统权限表 格式:grant 权限 on 数据库.* to 用户名@登陆主机 identified by "密码";
2.4 若是想指定部分权限给一用户,能够这样来写: mysql>grant select,update on testDB.* to test@localhost identified by '1234'; mysql>flush privileges; //刷新系统权限表
2.5 受权test用户拥有全部数据库的某些权限: mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234"; //test用户对全部数据库都有select,delete,update,create,drop 权限。 //@"%" 表示对全部非本地主机受权,不包括localhost。 (localhost地址设为127.0.0.1,若是设为真实的本地地址,不知道是否能够,没有验证。) //对localhost受权: 加上一句grant all privileges on testDB.* to test@localhost identified by '1234';便可。
- 三、删除用户
@>mysql -u root -p @>密码 mysql>Delete FROM user Where User='test' and Host='localhost'; mysql>flush privileges; mysql>drop database testDB; //删除用户的数据库 删除帐户及权限:>drop user 用户名@'%'; >drop user 用户名@ localhost;
- 其余mysql操做
5. 列出全部数据库 mysql>show database; 6. 切换数据库 mysql>use '数据库名'; 7. 列出全部表 mysql>show tables; 8. 显示数据表结构 mysql>describe 表名; 9. 删除数据库和数据表 mysql>drop database 数据库名; mysql>drop table 数据表名;
十一、主从服务器测试
主服务器Mysql,创建数据库,并在这个库中建表插入一条数据:
mysql> create database hi_db;
Query OK, 1 row affected (0.00 sec) mysql> use hi_db; Database changed mysql> create table hi_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into hi_tb values(001,'bobu'); Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) 从服务器Mysql查询: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | //I'M here,你们看到了吧 | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use hi_db Database changed mysql> select * from hi_tb; //查看主服务器上新增的具体数据 +------+------+ | id | name | +------+------+ | 1 | bobu | +------+------+ 1 row in set (0.00 sec)
十二、完成:
编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就代表主从有问题了,发短信警报吧。