Centos7安裝Mysql(Mariadb)


一:安裝方式

yum安裝(配置yum源)

	1-  centos官方的yum源
	
	
	#不同的yum源,軟件包的名字也可能不一樣,區分大小寫 !!!!

	
	2-  阿里雲的yum源(下載速度快,但是版本較低)
		安裝命令如下:
		(由於網速問題,我選擇用阿里雲的精簡版)
		yum install mariadb-server  mariadb
	
		
		
	3-  mysql官方的yum源 (版本你自由去選擇,下載較慢,文件完整性最好)
	
	配置如下方式
		1.找到yum倉庫目錄,創建repo文件
			touch /etc/yum.repos.d/mariadb.repo 			
					
		[root@bogon ~]# cd /etc/yum.repos.d
		[root@bogon yum.repos.d]# ls
		CentOS-Base.repo  epel.repo  repobak
		[root@bogon yum.repos.d]# touch mariadb.repo 
		
		
		2.添加repo倉庫配置 :寫入如下內容,指定mysql官方的yum源
			
			[root@bogon yum.repos.d]# vim mariadb.repo 
		
				[mariadb]
				name = MariaDB
				baseurl = http://yum.mariadb.org/10.1/centos7-amd64
				gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
				gpgcheck=1
				
		3.此時yum會自動的加載這個repo文件,讀取內容
		
		4.下載mariadb數據庫,服務端和客戶端
		yum install MariaDB-server MariaDB-client
         # 或者
		yum install MariaDB-server MariaDB-client -y
		
	
	
	yum源就是配置在/etc/yum.repos.d/底下的*.repo文件而已


二:啟動mariadb數據庫

#啟動MariaDB
systemctl start mariadb

# 啟動以后 查看有沒有mysql進程
ps -ef | grep mysql

# 查看mysql的網絡信息
netstat -tunlp | grep 3306

[root@bogon yum.repos.d]# netstat -tunlp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      86177/mysqld  


mysql -uroot -p


通過客戶端連接數據庫
mysql有哪些種類的客戶端?

mysql -uroot -p
pymysql
navicat(可視化工具)


啟動mariadb相關命令

mariadb數據庫的相關命令是:

systemctl start mariadb  #啟動MariaDB

systemctl stop mariadb  #停止MariaDB

systemctl restart mariadb  #重啟MariaDB

systemctl enable mariadb  #設置開機啟動



三:初始化mysql

3.1:詳解


在確認 MariaDB 數據庫軟件程序安裝完畢並成功啟動后請不要立即使用。為了確保數據 庫的安全性和正常運轉,需要先對數據庫程序進行初始化操作。這個初始化操作涉及下面 5 個 步驟。
➢ 設置 root 管理員在數據庫中的密碼值(注意,該密碼並非 root 管理員在系統中的密 碼,這里的密碼值默認應該為空,可直接按回車鍵)。
➢ 設置 root 管理員在數據庫中的專有密碼。
➢ 隨后刪除匿名賬戶,並使用 root 管理員從遠程登錄數據庫,以確保數據庫上運行的業
務的安全性。
➢ 刪除默認的測試數據庫,取消測試數據庫的一系列訪問權限。
➢ 刷新授權列表,讓初始化的設定立即生效。


確保mariadb服務器啟動后,執行命令初始化

mysql_secure_installation


3.2:配置信息

# 默認回車
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 MariaDB
root user without the proper authorisation.

# 設置密碼 123
Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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.

#移出匿名用戶 yes
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] n
 ... skipping.

By default, MariaDB 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 MariaDB
installation should now be secure.

Thanks for using MariaDB!


3.3:配置信息圖片

img

img

img

img

img

img



四:配置mysql


4.1:中文編碼設置

1.中文編碼設置,編輯mysql配置文件/etc/my.cnf,下入以下內容
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8



修改mariadb數據庫的的中文支持
# 一定要在創建數據信息 提前設置這一步 ,要不然會顯示拉丁文,在修改會有一些麻煩

\s  查看數據庫編碼

# \s 查看的詳細信息內容
"""
MariaDB [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		11
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		5.5.60-MariaDB MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1      # 為拉丁文
Db     characterset:	latin1		# 為拉丁文
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			10 min 7 sec

Threads: 1  Questions: 29  Slow queries: 0  Opens: 1  Flush tables: 2  Open tables: 27  Queries per second avg: 0.047
--------------

"""



修改mysql的配置文件,讓它支持中文
通過yum安裝的配置文件在/etc/my.cnf

vim /etc/my.cnf

配置內容如下:
#服務端的編碼添加如下內容
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

# datadir=/var/lib/mysql
# socket=/var/lib/mysql/mysql.sock
# 自帶的這兩行不刪除


#客戶端的編碼如下
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

#重啟mariadb生效編碼
systemctl restart mariadb



# [root@bogon yum.repos.d]# mysql -uroot -p
# Enter password: 

# MariaDB [(none)]> \s
# 查看編碼變為utf8

"""
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8

"""


# 備注
# 如果在創建的時候沒有換編碼 寫入的數據都是拉丁文的、

# 修改為拉丁文的數據庫的編碼
alter database 數據庫名字 default character set 'utf8'

# 修改為拉丁文的表的編碼
alte table 表名字 default character set 'utf8'

# 發現 表里的數據還是拉丁文  因為表字段的類型還是拉丁文

# 修改字段的編碼
alter table 表名 modify name varchar(30) character set 'ut-8'

# 發現以前表里為拉丁文的數據 還是拉丁文
# 但是此時 新建的數據都是 正常的 utf8 了



4.2:數據庫權限設置

mysql使用grant命令對賬戶進行授權,grant命令常見格式如下

grant 權限 on 數據庫.表名 to 賬戶@主機名            對特定數據庫中的特定表授權
grant 權限 on 數據庫.* to 賬戶@主機名              對特定數據庫中的所有表給與授權
grant 權限1,權限2,權限3 on *.* to 賬戶@主機名      對所有庫中的所有表給與多個授權
grant all privileges on *.* to 賬戶@主機名      對所有庫和所有表授權所有權限

退出數據庫,使用root登錄,開始權限設置

[root@master ~]# mysql -uroot -p

MariaDB [(none)]> use mysql;

MariaDB [(none)]> grant all privileges on *.* to ivan@127.0.0.1;

MariaDB [mysql]> show grants for ivan@127.0.0.1;

移除權限

MariaDB [(none)]> revoke all privileges on *.* from ivan@127.0.0.1;

配置mysql

遠程連接設置哦設置所有庫,所有表的所有權限,賦值權限給所有ip地址的root用戶
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by 'password';

grant all privileges on *.* to root@'%' identified by '123';

[root@bogon yum.repos.d]# mysql -uroot -p
Enter password: 123

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)



#創建用戶
MariaDB [(none)]> create user 'username'@'%' identified by 'password';

#刷新權限
flush privileges;






五:mysql基本命令

#修改mysql密碼
MariaDB [(none)]> set password = PASSWORD('redhat123');

生產環境里不會死磕root用戶,為了數據庫的安全以及和其他用戶協同管理數據庫,就需要創建其他數據庫賬戶,然后分配權限,滿足工作需求。

MariaDB [(none)]> create user ivan@'127.0.0.1' identified by 'redhat123';

MariaDB [(none)]> use mysql;

MariaDB [mysql]> select host,user,password from user where user='ivan';

切換普通用戶yuchao,查看數據庫信息,發現無法看到完整的數據庫列表

[root@master ~]# mysql -uivan -p -h 127.0.0.1

MariaDB [(none)]> show databases;
# 准備數據 先創建一個庫 一個表格

# 創建庫
MariaDB [(none)]> create databases t1
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'databases t1' at line 1
MariaDB [(none)]> create database t1;
Query OK, 1 row affected (0.00 sec)


MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| t1                 |
+--------------------+
4 rows in set (0.00 sec)


# 創建一個表
MariaDB [(none)]> use t1
Database changed
MariaDB [t1]> create table student(id int,name varchar(50));
Query OK, 0 rows affected (0.00 sec)

# 插入數據
MariaDB [t1]> insert into student values(1,'ivan');
Query OK, 1 row affected (0.00 sec)

MariaDB [t1]> select * from student;
    
+------+------+
| id   | name |
+------+------+
|    1 | ivan |
+------+------+
1 row in set (0.01 sec)


六:數據庫備份與恢復

6.1:備份數據庫數據

# mysqldump命令用於備份數據庫數據

mysqldump -u root -p --all-databases > /tmp/db.dump

導出db1、db2兩個數據庫的所有數據
mysqldump -uroot -proot --databases db1 db2 >/tmp/user.sql


# 備份命令:
mysqldump  -uroot -p --all-databases >  /opt/alldb.sql

[root@bogon ]# mysqldump  -uroot -p --all-databases >  /opt/alldb.sqlEnter password: 
[root@bogon ]# cd /opt/
[root@bogon opt]# ls
!                       luffy_boy                     Python-3.6.2
07-luffy_project_01      luffy_boy.zip                 Python-3.6.2.tgz
07-luffy_project_01.zip  __MACOSX                      s20static
alldb.sql                node-v8.6.0-linux-x64         tengine-2.3.1
allenv                   node-v8.6.0-linux-x64.tar.gz  tengine-2.3.1.tar.gz
crm                      python36   



6.2:先刪除數據庫

此為測試,所以先刪除一個

進入mariadb數據庫,刪除一個db
[root@bogon ]# mysql -uroot -p

MariaDB [(none)]> drop database t1
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
    
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> source /opt/alldb.sql
MariaDB [t1]> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| t1                 |
+--------------------+

6.3:數據恢復

方式1:
進入數據庫后,用source命令讀取sql文件
[mysql] >  source /opt/alldb.sql

方式2:
用登錄命令導入數據     把剛才重定向備份的數據庫文件導入到mysql中
mysql -uroot -p < /opt/alldb.sql 

[root@bogon opt]# mysql -uroot -p < /opt/alldb.sql 
Enter password: 

方式3:
當數據量特別大的時候,以上方式不推薦,使用第三方工具進行導入


七:MYSQL主從復制

7.1:詳解

MySQL數據庫的主從復制方案,是其自帶的功能,並且主從復制並不是復制磁盤上的數據庫文件,而是通過binlog日志復制到需要同步的從服務器上。

MySQL數據庫支持單向、雙向、鏈式級聯,等不同業務場景的復制。在復制的過程中,一台服務器充當主服務器(Master),接收來自用戶的內容更新,而一個或多個其他的服務器充當從服務器(slave),接收來自Master上binlog文件的日志內容,解析出SQL,重新更新到Slave,使得主從服務器數據達到一致。

主從復制的邏輯有以下幾種

一主一從,單向主從同步模式,只能在Master端寫入數據

一主多從

img

雙主主復制邏輯架構,此架構可以在Master1或Master2進行數據寫入,或者兩端同事寫入(特殊設置)

img

在生產環境中,MySQL主從復制都是異步的復制方式,即不是嚴格的實時復制,但是給用戶的體驗都是實時的。
MySQL主從復制集群功能使得MySQL數據庫支持大規模高並發讀寫成為可能,且有效的保護了服務器宕機的數據備份。

應用場景

利用復制功能當Master服務器出現問題時,我們可以人工的切換到從服務器繼續提供服務,此時服務器的數據和宕機時的數據幾乎完全一致。
復制功能也可用作數據備份,但是如果人為的執行drop,delete等語句刪除,那么從庫的備份功能也就失效了.

主從機制實現原理

img

(1) master將改變記錄到二進制日志(binary log)中(這些記錄叫做二進制日志事件,binary log events); 
(2) slave將master的binary log events拷貝到它的中繼日志(relay log); 
(3) slave重做中繼日志中的事件,將改變反映它自己的數據。

7.2:master主庫配置

環境准備,兩台機器分別安裝好mariadb數據庫


機器1:192.168.190.11   (master機器)
機器2:192.168.190.146  (slave機器)

#查看數據庫狀態
systemctl status mariadb
#停mariadb
systemctl stop mariadb

1.修改mysql的配置文件,開啟binlog日志功能
vim /etc/my.cnf,寫入如下信息

vim /etc/my.cnf

[mysqld]
server-id=1
log-bin=s20mysql-bin
# s20mysql-bin 這個名字隨便起

修改完如下:
[mysqld]
server-id=1
log-bin=mysql-bin
character-set-server=utf8
collation-server=utf8_general_ci



2.重啟數據庫生效binlog日志文件
systemctl start mariadb

[root@bogon opt]# mysql -uroot -p
Enter password: 
MariaDB [(none)]> show master status;
Empty set (0.00 sec)



MariaDB [(none)]> show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin                         | OFF   |
| log_bin_trust_function_creators | OFF   |
| sql_log_bin                     | ON    |
+---------------------------------+-------+
3 rows in set (0.00 sec)



# 遇到一個坑  配置完vim /etc/my.cnf

# systemctl start mariadb 重啟數據  還是不顯示 
# MariaDB [(none)]> show master status;
# Empty set (0.00 sec)

# 把數據庫停止在重新開 就好了
#停mariadb
systemctl stop mariadb

# 再次查看狀態信息
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


MariaDB [(none)]> show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin                         | ON    |
| log_bin_trust_function_creators | OFF   |
| sql_log_bin                     | ON    |
+---------------------------------+-------+
3 rows in set (0.00 sec)


3.在主庫上,創建一個用戶,用於主從復制

新建用於主從同步的用戶ivan,允許登錄的從庫是'%' 所有
create user 'ivan'@'%' identified by 'ivan123';

MariaDB [t1]> create user 'ivan'@'%' identified by 'ivan123';
Query OK, 0 rows affected (0.00 sec)


# 補充
新建用於主從同步的用戶ivan,允許登錄的從庫是'192.168.178.130'
create user 'ivan'@'192.168.178.130' identified by 'redhat';

# 題外話:如果提示密碼太簡單不復合策略加在前面加這句
mysql> set global validate_password_policy=0;
4.給這個ivan賬號,授予slave的身份
grant replication slave on *.* to 'ivan'@'%';

MariaDB [t1]> grant replication slave on *.* to 'ivan'@'%';
Query OK, 0 rows affected (0.00 sec)


#
.給從庫賬號授權,說明給ivan從庫復制的權限,在192.168.178.130機器上復制
grant replication slave on *.* to 'ivan'@'192.168.178.130';
#檢查主庫創建的復制賬號
select user,host from mysql.user;
#檢查授權賬號的權限
show grants for ivan@'192.168.178.130';
5.進行鎖表,防止數據寫入
# 實現對主數據庫鎖表只讀,防止數據寫入,數據復制失敗

flush table with read lock;
# 鎖上 就沒法 crate database了

# 沒有鎖表之前 插入數據 就會變位置信息
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> create database t2;
Query OK, 1 row affected (0.00 sec)

# 檢查主庫的狀態
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      324 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

# File是二進制日志文件名,Position 是日志開始的位置。

MariaDB [(none)]> flush table with read lock;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> create database t3;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
# 鎖上 就沒法 crate database了
6.導出此時的主庫數據,發送給從庫,保證起點一致性
mysqldump -uroot -p --all-databases > /tmp/alldb.sql

scp  /tmp/alldb.sql root@192.168.16.49:/tmp/
scp  /tmp/alldb.sql root@192.168.190.146:/tmp/


[root@bogon opt]# mysqldump -uroot -p --all-databases > /tmp/alldb.sql
Enter password:  # 密碼是數據庫的密碼 123


[root@bogon opt]# scp  /tmp/alldb.sql root@192.168.190.146:/tmp/
root@192.168.190.146's password: 
alldb.sql                                     100%  504KB   4.6MB/s   00:00    
[root@bogon opt]# 
# 此步驟 注意傳輸的文件大小 如果傳輸的為0k ,需要重新傳輸

7.當從庫配置好復制之后,回到這里解鎖,寫入數據,查看從庫數據是否寫入

unlock tables;

MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)




7.3:slave從庫配置

機器2:192.168.190.146  (slave主機)



1.修改從庫機器的配置文件,開啟id,以及只讀模式
vim /etc/my.cnf 如下

[mysqld]
server-id=1000
read-only=true


# 修改完如下:
[mysqld]
server-id=1000
read-only=true

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



# 設置server-id值並關閉binlog功能參數
# 數據庫的server-id在主從復制體系內是唯一的,Slave的server-id要與主庫和其他從庫不同,並且注釋掉Slave的binlog參數。
2.重啟從庫
systemctl restart mariadb

#1 把主發過來的導入過來
[root@bogon yum.repos.d]# mysql -uroot -p < /tmp/alldb.sql 
Enter password: 
# 此為數據庫的密碼 當前沒有配置密碼 直接回車就ok


# 2 查看導入過來的數據成功
[root@bogon tmp]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| t1                 |
| t2                 |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> 

# 默認的多一個test數據庫 ,刪除即可
MariaDB [(none)]> drop database test;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| t1                 |
| t2                 |
+--------------------+
5 rows in set (0.00 sec)

# 此時把 主的數據庫的密碼也導入過來了  
# 進入數據庫的時候需要輸入主庫的密碼


[root@bogon tmp]# mysql -uroot -p
Enter password:  
# 進入數據庫的時候需要輸入主的密碼


# 檢查Slava從數據庫的各項參數
show variables like 'log_bin';
show variables like 'server_id';


MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | OFF   |
+---------------+-------+
1 row in set (0.00 sec)



3.一條命令,建立主從之間的復制關系
# 配置復制的參數,Slave從庫連接Master主庫的配置


change master to master_host='192.168.190.11',
master_user='ivan',
master_password='ivan123',
master_log_file='mysql-bin.000001',
master_log_pos=324;
# 主庫的名字
# 主庫的位置 起始點

MariaDB [(none)]> change master to master_host='192.168.190.11',
    -> master_user='ivan',
    -> master_password='ivan123',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=324;
Query OK, 0 rows affected (0.06 sec)
4.開啟從庫的slave功能
start slave;

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
5.查看從庫的狀態,檢測是否復制成功

show slave status\G 

"""
MariaDB [(none)]> show slave status\G 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.190.11
                  Master_User: ivan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 324
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
....

"""


查看如下兩個參數是否是yes,主從復制即為正確

			Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

# 跳到主庫配置第7部 解鎖 寫入數據  查看是否同步成功


注意一個問題:
# 此時從數據庫是用root登錄的 ,在從數據庫創建數據 會創建成功,root的權限比較大
# 但是在主數據庫不會顯示,因為不會同步

解決
在主數據庫上面操作
grant select on  *.*  to ivan@"%";

MariaDB [(none)]> grant select on  *.*  to ivan@"%";
Query OK, 0 rows affected (0.00 sec)


從數據庫:

[root@bogon tmp]# mysql -uivan -p
Enter password: 

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| 同步成功           |
| mysql              |
| performance_schema |
| t1                 |
| t2                 |
+--------------------+
6 rows in set (0.00 sec)


MariaDB [(none)]> create database t3;
ERROR 1290 (HY000): The MariaDB server is running with the --read-only option so it cannot execute this statement
MariaDB [(none)]> 

tip:

注意此處還未配置從庫的只讀模式,只需在slave服務器上配置/etc/my.cnf,加上以下配置,並且在slave上創建普通用戶,使用普通用戶主從同步即可達到只讀的效果

如果用root用戶,無法達到readonly,這是一個坑
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
server-id=3
read-only=true
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM